Several examples of data read and write IO C language

fgetc int (the FILE * Stream)
int of fputc (int CH, the FILE * Stream)
fgetc () function returns a character from the current position of the input stream, and the file pointer to the next character pointer at the end of the file if, function returns the EOF, at this time represents the end of this operation, when the completion of reading and writing files, the file should be closed.
of fputc () function writes a value at the completion of the character ch stream file specified by the current position, and the file pointer after a move. of fputc () function return value is the value of the written characters, returns EOF error.

the fscanf int (Stream the FILE *, char * the format, arg_list)
int fprintf (Stream the FILE *, char * the format, arg_list)
file format is read by a letter in front of the above-described function f becomes the fscanf () and fprintf ( ) 
wherein, stream file pointer for the stream, and the remaining two parameters scanf () and printf () use the same.

char * fgets (char * str, int num, FILE * stream) // row of data read feeling
int fputs (char * STR, the FILE * stream)
fgets () function reads the stream from the stream file in at most num-1 characters. and put them into the character array str points to. Reads characters encountered until the carriage return or EOF (end of file) until the, or read the character defined.
fputs () function writes the string pointed str stream file. When the operation is successful, the function returns 0, otherwise return a non-zero value. 

fread int (void * buf, int size, int COUNT, the FILE * stream)
int fwrite (void * buf, int size, int COUNT, the FILE * stream)
fread () function reads from the stream file stream directed count (the number of fields ) fields, each field size (field length) characters long, and put them buf (character array pointed to by buffer).
fread () function returns the number of fields actually read. If the required number of function calls to read field exceeds the number of fields stored in the file, an error or the end of the file, the detection should be noted that the actual operation.
fwrite function from the character array buf (buffer) points () in the count (number of fields) fields written stream pointed stream, each field size characters long, the function returns a successful operation fields when written number.
About read and write files into blocks, you can only create binary file format when creating the file.

 

Typically, for more complex format of the input data, we can take as a data string input in various formats, and then converts the string to the desired format. C provides the function:
int atoi (char * PTR)
a float the atof (char * PTR)
Long int atol (char * PTR)
respectively convert the string to integer, real, and long integer. Please use the math.h header file which contains or stdlib.h written before the program.

 

Copy the code

#include "stdafx.h" 
#include <stdio.h> 

// will be stored on the disk to read and write files in the specified text characters one by one, to read from the file, and then display it on the screen. 
main int (int argc, char * argv []) 
{ 
    char CH; 
    the FILE * FP; 
    int I; 

    IF ((FP = the fopen (argv [. 1], "R & lt")) == NULL) / * open a by the argv [1] referring to the file * / 
    { 
        the printf ( "Not Open"); 
        return 0; 
    } 

    the while (! (CH = fgetc (FP)) = the EOF) / * a character read from the file, to the screen * / { 
        putchar (ch); // If you print the same time printf ( "(% d)" , ch); putchar Chinese can not be displayed correctly 
    } 

    fclose (fp); 
    printf ( "the Hello World \ the n-!"); 
    return 0; 
}

Copy the code

Copy the code

#include "stdafx.h" 
#include <stdio.h> 
#include <string.h> 

// write string to the disk, and writes a text file test.txt: 
int main (int argc, char * argv [] ) 
{ 
    the FILE * FP; 
    char STR [128]; 

    IF ((FP = the fopen ( "test.txt", "W")) == NULL) 
    { 
        the printf ( "Not Open"); 
        return 0; 
    } 

    the while (( ! strlen (gets (str)) ) = 0) / * If the string length is zero, then the end * / 
    { 
        fputs (STR, FP); / * write a string * / 
        fputs ( "\ n-", FP); / * write a carriage return * / 
    } 

    fclose (FP); 
    the printf ( "the Hello World \ n-!"); 
    return 0; 
}

Copy the code

Copy the code

#include "the stdafx.h" 
#include <stdio.h> 

// some writing data formatted text file, and from which file to read formatting method displayed on the screen, 
// formatted data two student records, including name, student number, two subjects results. 
int main (int argc, char * the argv []) 
{ 
    the FILE * FP; 
    int I; 
    struct STU {/ * definition of the structure type * / 
        char name [15]; 
        char NUM [. 6]; 
        a float Score [2]; 
    } student; / * DESCRIPTION structure variables * / 

    IF ((FP = the fopen ( "test.txt", "W")) == NULL) / * open a text file writing only * / 
    { 
        the printf ( "CAN Not open file "); 
        return 0; 
    } 
    the printf (" INPUT Data: \ n-"); 
    for (I = 0; I <2; I ++) 
    { 
        Scanf ("% S% F% F% S ", student.name, Student. num,
            & student.score [1]); / * input from keyboard * / 
        fprintf (FP, "% S% S% 7.2f% 7.2f \ n-", student.name, student.num, 
            student.score [0], 
            Student .score [1]); / * write files * / 
    } 
    fclose (FP); / * Close the file * / 

    IF ((FP = the fopen ( "test.txt", "R & lt")) == NULL) / * read-only reopen the text file * / 
    { 
        the printf ( "CAN Not open file"); 
        return 0; 
    } 

    the printf ( "Output from file: \ n-"); 
    // the fscanf scanning feature is only fixed scan format, Whether wrap does not wrap. If a line scan only half the data is then scanned back below 
    while (fscanf (fp, "% s% s% f% f \ n", student.name, student.num, & student.score [0], & student.score [1])! = EOF) / * read from the file * / {
        printf ( "% s% s% 7.2f% 7.2f \ n", student.name, student.num, student.score [0], student.score [1]); / * Display to the screen * / 
    } 

    fclose ( fp); / * close file * / 
    
    printf ( "the Hello World \ the n-!"); 
    return 0; 
}

Copy the code

 

Copy the code

#include "the stdafx.h" 

int main (int argc, char * the argv []) 
{ 
    the FILE * FP1; 
    int I; 
    struct STU {/ * definition of the structure * / 
        char name [15]; 
        char NUM [. 6]; 
        a float Score [2]; 
    } Student; 

    IF ((FP1 = the fopen ( "test.txt", "WB")) == NULL) / * in binary writing only open file * / 
    { 
        the printf ( "CAN Not open file") ; 
        return 0; 
    } 
    the printf ( "INPUT Data: \ n-"); 
    for (I = 0; I <2; I ++) 
    { 
        Scanf ( "% S% F% F% S", student.name, student.num, & student.score [0], & student.score [1]); / * input a record * / 
        fwrite (& Student, the sizeof (Student),. 1, FP1);/ * Block is written into the file * / 
    } 
    fclose (FP1);

    if ((fp1 = fopen ( " test.txt", "rb")) == NULL) / * write only to re-open the file in binary * / 
    { 
        printf ( "CAN not Open File"); 
        return 0; 
    } 
    printf ( " Output from file: \ n-"); 
    for (I = 0; I <2; I ++) 
    { 
        fread (& Student, the sizeof (Student),. 1, FP1); / * block read from the file to a * / 
        the printf ("% S % s% 7.2f% 7.2f \ n ", student.name, student.num, student.score [0], student.score [1]); / * display to the screen * / 
    } 
    fclose (FP1); 
    
    the printf ( "! the Hello World \ the n-"); 
    return 0; 
}

Copy the code


Another example of life and death is not, please advise:

Copy the code

#include "stdafx.h" 
#include <stdio.h> 
#include <string.h> 

// write string to the disk, and writes a text file test.txt: 
int main (int argc, char * argv [] ) 
{ 
    the fILE * FP1, FP2 *; 
    char STR [128]; 
    IF ((FP1 = the fopen ( "the test1.txt", "R & lt")) == NULL) / * Read-only files *. 1 / 
    { 
        the printf ( "CAN Not open file \ n-"); 
        return 0; 
    } 

    IF ((FP2 = the fopen ( "is test2.txt", "W")) == NULL) / * open file for writing only * 2 / 
    { 
        the printf ( "CAN Not Open File \ n-"); 
        return 0; 
    } 

    the while ((strlen (fgets (STR, 128, FP1)))> 0) 
    { 
        fputs (STR, FP2);/ * Read the string from the file and writes the file 1 * 2 / 
        the printf ( "% S", STR); / * in * screen display / 
    }
    fclose(fp1);
    fclose(fp2);
}

Copy the code

Published 407 original articles · won praise 150 · views 380 000 +

Guess you like

Origin blog.csdn.net/ds1130071727/article/details/103387582