Example one, save the short phrase "Hello world!" to the disk file F1.txt.
#include <stdio.h>#include<stdlib.h>intMain) {FILE*FP;/*Defining file Pointers*/ if(fp = fopen ("F1.txt","W")) = = NULL) {/*Open File*/printf ("File Open error!\n"); Exit (0); } fprintf (FP,"%s","Hello world!");/*Write a file*/ if(fclose (FP)) {/*Close File*/printf ("Can not close the file!\n" ); Exit (0); } return 0;}
Use a file pointer to indicate where the file buffer is read and written
FILE *FP;
Custom Type (typedef):
1. Rename the existing types in C (including custom types that have already been defined)
2, the new name can replace the existing data type
3. Used to simplify the description of complex data type definitions
typedef < existing type name > < new type name >;
typedef int INTEGER; int I, J; <====> INTEGER i, J; typedef int* point ; int* p1; <====> point p1;
How to use a custom type (typedef)
1. Define variable int i
2, variable name? new type name int? INTEGER
3, plus typedef typedef INT INTEGER
4. Define the variable INTEGER I with the new type name;
int NUM[10]
typedef int NUM[10]
NUM a <===> int a[10]
Example Two
It is known that a data file F.txt saved 5 students ' computer grade test scores, including the number, name and score, the file contents are as follows, please read out the contents of the file and display it to the screen.
301101 Wenhe Zhang 91
301102 Chen Hui 85
301103 Wdwang 76
301104 Zheng Wei 69
301105 Guo Wentao 55
#include"stdio.h"intMainvoid) {FILE* FP;/*Defining file Pointers*/ Longnum; Charstname[ -]; intscore; if(fp = fopen ("F.txt","R")) = = NULL) {/*Open File*/printf ("File Open error!\n"); Exit (0); } while( !feof (FP)) {fscanf (FP),"%ld%s%d", &num, Stname, &score); printf ("%ld%s%d\n", NUM, stname, score); }; if(fclose (FP)) {/*Close File*/printf ("Can not close the file!\n" ); Exit (0); }}
if(fp = fopen ("F.txt","R")) ==NULL) {printf ("File Open error!\n"); Exit (0);} fopen ("file name","how files are opened") function fopen () returns a value of 1, executes successfully, returns a file-type address that contains information such as a buffer of files, assigns a file pointer fp2, is unsuccessful, returns a null (null value) exit (0): Closes all open files and terminates the program's execution parameter 0 to indicate that the program is ending normally; a non-0 parameter usually indicates an unhealthy program end
fp = fopen ("F.txt", "R") file open with parameter table
File read-write and open mode
ifthe file specified by the read file must exist, otherwise an error occurs;ifWrite a file (the specified file may or may not exist)ifTo"W"Way to writeifthe document already exists and the original document will be deleted to be re-established;Elsecreates a new file by the specified name;Else ifTo"a"Way to writeifthe file already exists to write the data will be added to the specified file after the original data, will not delete the original content;Elsecreates a new file by the specified name (same as "w");iffile simultaneous read and write use"r+"、"w+"Or"A +"Open File
Close File
if (fclose (FP)) { "Can not close the file!\n" ); Exit (0);} The fclose (file pointer) writes the data in the buffer to the disk sector, ensuring that the normal completion of the write file frees the file buffer unit and the document structure, decoupling the file pointer from the specific file. return value of function fclose () returns 0: Normal close file returned non 0: Unable to close file properly
File copy
If you know a text data file F1.txt, copy the file and save it as F2.txt.
Create a new text file F1.txt, place the file in the same directory as the source program, execute the program, and observe the results.
#include <stdio.h>intMainvoid) {FILE*fp1,*FP2; CharC; if(FP1 = fopen ("F1.txt","R")) ==NULL) {printf ("File Open error!\n" ); return 0; } if(FP2 = fopen ("F2.txt","W")) ==NULL) {printf ("File Open error!\n" ); return 0; } while( !feof (FP1)) {C=fgetc (FP1); FPUTC (c, FP2); } fclose (FP1); Fclose (FP2); return 0;}
Open multiple Files
if(FP1 = fopen (F1.dat,"R")) ==NULL) {printf ("File Open error!\n"); Exit (0);}if((Fp2=fopen ("F2.dat","W")) ==NULL) {printf ("File Open error!\n"); Exit (0);} C language allows to open multiple files at the same time different files corresponding to different file pointers do not allow the same file to open again before closing
C language data input output to file operation Freopen () function (1)