In the file Input.csv file, we have the following data
Applepear Litchispineapplewatermelon
Now we will read and write to the Output.csv file under the Input.csv file, we will use the fopen function
Function Prototypes: FILE * fopen (const char * path,const char * mode)
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5 intMain () {6 Charline[ the];7FILE *inch=fopen ("Input.csv","R");//fopen can create a data stream; R, which indicates the read8FILE * out=fopen ("Output.csv","a");//A, indicating append data to the file9 while(FSCANF (inch,"%79[^\n]\n", line) = =1){Tenfprintf ( out,"From input:%s\n", line); One } A //after you run out of data streams, you need to close them, even if they are shut down, because normally a process can have up to 256 data streams, and the number is limited -Fcloseinch); -Fclose out); the return 0; -}
Fopen also has many patterns, such as
W, writes the file, if the file does not exist, creates the file and writes it, if the file exists, overwrites the previous data
There are a+,w+,r+ and so on, but some compilers do not support, you can refer to here fopen
C language Learning 010:fopen read and write files