1, C
1.fopen function
std::FILE* fopen( const char* filename, const char* mode );
Open the file filename and return the file stream associated with the file. Mode is used to determine the file access mode.
parameter
filename | - | The file name to which the file stream is associated |
mode | - | A null terminated string determines the file access mode |
Return value
If successful, a pointer to the object that controls the open file stream is returned, and the eof and error bits are cleared at the same time. When an error occurs, a null pointer is returned.
2.fclose
int fclose(FILE *stream)
The C library function int fclose(FILE *stream) closes the stream. Flush all buffers.
parameter
Stream is a pointer to a FILE object that specifies the stream to be closed.
Return value
If the flow closes successfully, the method returns zero. EOF if failed.
3.FILE structure
Stdio in C language The structure FILE used for FILE operation is defined in the H header FILE. FILE operation is performed by fopen returning a FILE pointer (pointer to the FILE structure). Can be in stdio View the definition of the FILE structure in the H header FILE, as follows:
But it seems to be hidden in visual studio. See https://docs.microsoft.com/en-us/cpp/porting/visual-cpp-change-history-2003-2015
4.fprintf() and fscanf() functions
int fprintf(FILE *stream, const char *format, ... ); fprintf()Function according to the specified format(format)Send message(parameter)To by stream(flow)Specified file. int fscanf(FILE *stream, const char *format, ...) C Library function int fscanf(FILE *stream, const char *format, ...) follow the prevailing fashion stream Read formatted input.
- Stream -- pointer to the FILE object that identifies the stream.
- Format -- a C string that contains one or more of the following: a space character, a non space character, and a format specifier.
If it succeeds, it returns the number of successful assignments or writes, and if it fails or reaches the end of the file, it returns a negative number.
2, C++
The header file fsstream defines three types to support file IO: i fstream reads data from a given file, ofstream writes data to a given file, and fsstream can read and write to a given file.
Each stream has an associated file mode that indicates how files are used. Table 8.4 lists the file patterns and their meanings.
Each file stream type defines a default file mode, which is used when no file mode is specified. Files associated with i fstream are opened in in mode by default, files associated with ofstream are opened in out mode by default, and files associated with fsstream are opened in in and out modes by default.
3, Sample program
#include<iostream> #include<string> #include<algorithm> #include<sstream> #include<fstream> #include<stdio.h> using namespace std; const int MAX_NUM = 100; int a[MAX_NUM]; int n = 2; int main(int argc, char* argv[]) { FILE* file4 = fopen("d.txt", "w"); if (!file4) { printf("file4 open error!\n"); return -1; } //fprintf(file4, "name age sex position\n"); int age, sex; char name[50], position[50]; printf("please input 2 date : name age sex position:\n"); while (n--) { scanf("%s %d %d %s", &name, &age, &sex, &position); fprintf(file4, "%s %d %d %s\n", name, age, sex, position); } fclose(file4); FILE* file5 = fopen("d.txt", "r"); if (!file5) { printf("file5 open error!"); return -1; } int m = 2, ans, ans1; while (m--) { fscanf(file5, "%s %d %d %s", &name, &age, &sex, &position); printf("%s %d %d %s\n", name, age, sex, position); } fclose(file5); fstream file1("d.txt"); if (!file1) { cout << "file1 open error! " << endl; return -1; } string tmp; vector<string>str; while (file1 >> tmp) { str.push_back(tmp); cout << tmp << endl; } ifstream file2("b.txt", ios::in); if (!file2) { cout << "file2 open error! " << endl; return -1; } ofstream file3("c.txt", ios::out); if (!file3) { cout << "file3 open error! " << endl; return -1; } while (n--) { file3 << n<<' '; } file1.close(); file2.close(); file3.close(); return 0; }
The input and output results are as follows: