C language File read-write function--FPUTC and fgetc__ functions

Source: Internet
Author: User

Original link: http://www.letuknowit.com/archives/96

Understand how to open the file in C language can be formally read and write files, files according to the type and divided into ASCII files and binary files, we start with a simple start, first look at the ASCII file (text file) is how to read.

An ASCII file (a text file) holds a sequence of characters that can be read through the entire file as long as it is read out in order, until the end of the file.
, writing to a file is the same thing. FPUTC function

The role of the FPUTC function is to write a character to a file in the form of:

FPUTC (ch,pfile);

Where Ch is the character to write, Pfile is a pointer to the file structure, and the fopen function opens the file to get the pfile.

Writing to a file may fail, but how do you know if it is correctly written to a file? It is time to look at the return value of the FPUTC function, and if the FPUTC function succeeds in writing the character to the file, its return value is the one that was written, and if it fails, it returns EOF (end of file). EOF is a symbolic constant, and EOF in stdio.h is defined as-1, so see EOF as a-1. fgetc function

Know what FPUTC is doing, fgetc Basic also know, this is from the file read a character function, its invocation form:

ch=fgetc (pFile);

The parameter pfile is the same as the parameter of the FPUTC function, except that a parameter ch runs to the return value here. When Fgetc successfully reads characters from a file, CH is the character read and ch=eof if the read fails.

In summary, EOF plays an important role in the reading and writing of files, and when the value of EOF is read, the program knows that the file has been read.

FPUTC function and Fgetc function Example demo

Say a lot of not as direct to paragraph program memory deep, the following ant to do an example program, read a text file data, and then write to another text file, we first prepared a content "I likewww.letuknowit.com" text file.

#include "stdio.h"
 
void Main ()
 {
     char ch;
     FILE *psrc,*pdes;
 
    PSRC = fopen ("Srcfile.txt", "R");    Open file read data
     pDes = fopen ("Desfile.txt", "w");    Open File Write Data
 
    if ((NULL==PSRC) | | (null==pdes))
     {
         printf ("Open file Failed,can ' t go on\n");
         return;
     }
 
    CH=FGETC (PSRC);    Reads the first character while
     (EOF!= ch)
     {
         FPUTC (ch,pdes);    Write character
         ch=fgetc (PSRC);    Read the next character
     }
 
    printf ("successed copy Srcfile to desfile\n");
 
    Fclose (PSRC); Don't forget to close
     fclose (pDes);//Ditto
 }


Before running the program, you need to create a new text file in the program's running directory, and write the contents casually. It is worth mentioning that theFPUTC function and the FGETC function support Chinese .

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.