First of all, remember that the free function is only used to read binary files, and fscanf can read text or binary files.
Fwrite / free will write / read files in binary form, such as int type in the example, and will be saved in numerical form. If you use Notepad and other programs to open, you will see unrecognized content.
fprintf/fscanf formats the data content as a string, and the actual content written to the file is the ASCII code of each character of the string. If it is opened in Notepad, the text content will be displayed.
Suppose, for example, an a[50] array in an example program, you don't need to use a circular statement when using fwrite / free (although it doesn't matter if you use a circular statement to read one at a time),
fwrite(a, sizeof(int), 50, fp);
The contents of a array in memory will be copied to the file;
fread(a, sizeof(int), 50, fp);
The 50 * sizeof(int) data in the file will be written to the a array.
fprintf/fscanf doesn't need to say much, that is, format the data into strings for reading and writing.
1: Purpose
Master the reading and writing mode of C language text file;
Master the C language binary file reading and writing mode;
Master the reading and writing mode of CPP text file;
Master the reading and writing mode of CPP binary file;
2: C language text file reading and writing
- Text file write
//Write TX in C mode void TxtWrite_Cmode() { //Preparation data int index[50] ; double x_pos[50], y_pos[50]; for(int i = 0; i < 50; i ++ ) { index[i] = i; x_pos[i] = rand()%1000 * 0.01 ; y_pos[i] = rand()%2000 * 0.01; } //Write out txt FILE * fid = fopen("txt_out.txt","w"); if(fid == NULL) { printf("Failed to write file!\n"); return; } for(int i = 0; i < 50; i ++ ) { fprintf(fid,"%03d\t%4.6lf\t%4.6lf\n",index[i],x_pos[i],y_pos[i]); } fclose(fid); }
- Text file read
//Using C mode to read Txt void TxtRead_Cmode() { FILE * fid = fopen("txt_out.txt","r"); if(fid == NULL) { printf("open%s fail","txt_out.txt"); return; } vector<int> index; vector<double> x_pos; vector<double> y_pos; int mode = 1; printf("mode Is 1, read in and output by character; mode For 2, read in output by line; mode For 3, know the data format, read in and output by line\n"); scanf("%d",&mode); if(mode == 1) { //Read in by character and output directly char ch; //Read character, judging by the rule that ch is not equal to EOF (end of file) while(EOF!=(ch= fgetc(fid))) printf("%c", ch); } else if(mode == 2) { char line[1024]; memset(line,0,1024); while(!feof(fid)) { fgets(line,1024,fid); printf("%s\n", line); //output } } else if(mode == 3) { //Know data format, read in and store output by line int index_tmp; double x_tmp, y_tmp; while(!feof(fid)) { fscanf(fid,"%d%lf%lf\n",&index_tmp, &x_tmp, &y_tmp); index.push_back(index_tmp); x_pos.push_back(x_tmp); y_pos.push_back(y_tmp); } for(int i = 0; i < index.size(); i++) printf("%04d\t%4.8lf\t%4.8lf\n",index[i], x_pos[i], y_pos[i]); } fclose(fid); }
3: C language binary file reading and writing
- Binary write
//Write binary file in C mode void DataWrite_CMode() { //Preparation data double pos[200]; for(int i = 0; i < 200; i ++ ) pos[i] = i ; //Writing data FILE *fid; fid = fopen("binary.dat","wb"); if(fid == NULL) { printf("Error writing out file"); return; } int mode = 1; printf("mode 1, write one by one; mode Is 2, write line by line\n"); scanf("%d",&mode); if(1==mode) { for(int i = 0; i < 200; i++) fwrite(&pos[i],sizeof(double),1,fid); } else if(2 == mode) { fwrite(pos, sizeof(double), 200, fid); } fclose(fid); }
2. Binary file reading
//Reading binary files in C mode void DataRead_CMode() { FILE *fid; fid = fopen("binary.dat","rb"); if(fid == NULL) { printf("Error reading file"); return; } int mode = 1; printf("mode For 1, know pos How many; mode For 2, I don't know pos How many?\n"); scanf("%d",&mode); if(1 == mode) { double pos[200]; fread(pos,sizeof(double),200,fid); for(int i = 0; i < 200; i++) printf("%lf\n", pos[i]); free(pos); } else if(2 == mode) { //Get file size fseek (fid , 0 , SEEK_END); long lSize = ftell (fid); rewind (fid); //Open up storage space int num = lSize/sizeof(double); double *pos = (double*) malloc (sizeof(double)*num); if (pos == NULL) { printf("Open space error"); return; } fread(pos,sizeof(double),num,fid); for(int i = 0; i < num; i++) printf("%lf\n", pos[i]); free(pos); //Free memory } fclose(fid); }
4: C + + text file reading and writing
- Text file write
//Write txt in CPP mode void TxtWrite_CPPmode() { //Preparation data int index[50] ; double x_pos[50], y_pos[50]; for(int i = 0; i < 50; i ++ ) { index[i] = i; x_pos[i] = rand()%1000 * 0.01 ; y_pos[i] = rand()%2000 * 0.01; } //Write out txt fstream f("txt_out.txt", ios::out); if(f.bad()) { cout << "Error opening file" << endl; return; } for(int i = 0; i < 50; i++) f << setw(5) << index[i] << "\t" << setw(10) << x_pos[i] <<"\t" <<setw(10)<< y_pos[i] << endl; f.close(); }
2. text file reading
//Read txt in CPP mode void TextRead_CPPmode() { fstream f; f.open("txt_out.txt",ios::in); //File open options: //ios::in = 0x01, / / read, create if the file does not exist (ifstream is opened by default) //ios::out = 0x02, / / for writing. If the file does not exist, create it. If the file already exists, clear the original content (the default opening method of ofstream) //ios::ate = 0x04, / / when the file is opened, the pointer is at the end of the file. Can change the position of the pointer, often used in conjunction with in and out //ios::app = 0x08, / / for writing. If the file does not exist, create it. If the file already exists, write new content after the content of the original file. The pointer position is always the last //ios::trunc = 0x10, / / truncate the file length to 0 before reading and writing (default) //ios::nocreate = 0x20, / / an error occurs when the file does not exist. It is often used in conjunction with in or app //ios::noreplace = 0x40, / / an error occurs when the file exists. It is often used in combination with out //ios::binary = 0x80 / / binary format file vector<int> index; vector<double> x_pos; vector<double> y_pos; if(!f) { cout << "Error opening file" << endl; return; } cout<<"mode Is 1, read in and output by character; mode For 2, read in output by line; mode For 3, know the data format, read in and output by line"<<endl; int mode = 1; cin>>mode; if(1== mode) { //Read in and output by byte char ch; while(EOF != (ch= f.get())) cout << ch; } else if(2 == mode) { //Read by line and display char line[128]; int numBytes; f.getline(line,128); cout << line << "\t" << endl ; f.getline(line,128); cout << line << "\t" << endl ; f.seekg(0,0); //Skip byte //Seekg (absolute position); / / absolute move, / / input stream operation //Seekg (relative position, reference position); / / relative operation //tellg(); / / returns the current pointer position while(!f.eof()) { //Use eof() function to check whether the file is read end f.getline(line,128); numBytes = f.gcount(); //Use gcount() to get the actual number of bytes read cout << line << "\t" << numBytes << "byte" << endl ; } } else if(3 == mode) { //If you know the data format, you can read it directly with > > int index_temp = 0; double x_pos_temp = 0, y_pos_temp = 0; while(!f.eof()) { f >> index_temp >> x_pos_temp >> y_pos_temp ; index.push_back(index_temp); x_pos.push_back(x_pos_temp); y_pos.push_back(y_pos_temp); cout << index_temp << "\t" << x_pos_temp << "\t" << y_pos_temp << endl; } } f.close(); }
5: C + + binary reading and writing
- Binary write
//Write binary file in CPP mode void DataWrite_CPPMode() { //Preparation data double pos[200]; for(int i = 0; i < 200; i ++ ) pos[i] = i ; //Writing data ofstream f("binary.dat",ios::binary); if(!f) { cout << "Failed to create file" <<endl; return; } f.write((char*)pos, 200*sizeof(double)); //fwrite is written in the form of char * to make a transformation f.close(); }
2. Binary file reading
//Reading binary files in CPP mode void DataRead_CPPMode() { double pos[200]; ifstream f("binary.dat", ios::binary); if(!f) { cout << "fail to read file" <<endl; return; } f.read((char*)pos,200*sizeof(double)); for(int i = 0; i < 200; i++) cout << pos[i] <<endl; f.close(); }
Six summary
-
C language read and write files are operated by FILE pointer, in which fprintf and fscanf are used for reading and writing text files, and free and fwrite are used for reading and writing binary files
-
C + + read and write files are operated by fsstream, i fstream and ofstream, text files are read and written by < < and > > and binary files are read and write by read and write