C: File Operations-read-write characters and read-write lines

Source: Internet
Author: User
Tags include requires

1. Read-write character function putc () and getc ()

These two functions are similar to the Putchar () and the GetChar () functions. Assuming FP is a file pointer, ch is a character variable,

ch = getc(fp);// ch = getchar();
putc(ch,fp);// putchar(ch);

C implementation that outputs the contents of a file (by character) to the standard output:

#include <stdio.h>
#include <stdlib.h>
/* 将文件内容(按字符)输出到标准输出 */
void read_ch(char * filename, char * mode)
{
    int ch;
    FILE * fp;
    if ((fp = fopen(filename,mode)) == NULL)
    {
        fprintf(stderr,"Can't open %s.\n",filename);
        exit(1);
    }
    while ((ch = getc(fp)) != EOF)
       putc(ch,stdout);
    fclose(fp);
}

2. Read-Write line function fgets () and fputs ()

The gets () function requires only one parameter (char *), while the fgets () function requires three parameters (char *, int, FILE *). The first parameter is the address used to store the input, the second parameter represents the maximum length of the string, and the last parameter is the file pointer. are usually:

Fgets (buf, MAX, FP);//gets (BUF);

The fgets () function reads after the first newline character it encounters, or reads less than one character from the maximum length of the string, or reads to EOF. Then it adds a ' ' to the end ', which makes up a string. (This is different from the gets (), it reads "\ n" and adds a ', ' and when read again, it discards ' \ n '). Similar to gets (), fgets () returns NULL when it encounters EOF, otherwise it returns the address passed to it.

The fputs () function accepts two parameters, (char *, FILE *). Unlike puts (), it does not automatically add ' \ n '. are usually:

Fputs (BUF, FP);//puts (BUF);

C implementation that outputs the contents of a file (by rows) to standard output:

#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 21
/* 将文件内容(按行)输出到标准输出 */
void read_line(char * filename, char * mode)
{
    char line[MAXLINE];
    FILE * fp;
    if ((fp = fopen(filename,mode)) == NULL)
    {
        fprintf(stderr,"Can't open %s.\n",filename);
        exit(1);
    }
    while (fgets(line, MAXLINE,fp) != NULL)
       fputs(line,stdout);
    fclose(fp);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.