fopen
Function prototype FILE * fopen (const char *path,cost char *mode)
Action: Opens a file that returns a pointer to the file
Parameter description: The first parameter is the file path and file name to open the file, and the second parameter indicates how the file is opened
Note: Mode has the following values:
R: Read-only mode open, file must exist
r+: Readable and writable, must exist
rb+: Open binary file, you can read and write
rt+: Open text file, read and write
W: Write only, file exists the file length of 0, the file does not exist to create the file
w+: Can read and write, file exists The file length of 0, the file does not exist to establish the file
A: Attach the way to open only write, does not exist to create the file, there is written data added to the end of the file, the EOF character reserved
A +: Additional way to open to read and write, there is no establishment of the file, there is written data added to the end of the file, the EOF character does not retain
WB: Open binary file, write only
wb+: Open or set up binary files, read and write
wt+: Open or create a text file that can be read and written
at+: Open a text file, read-write, write data added to the end of the text
ab+: Open binary files, read and write, written data added to the end of the file
By the mode character, the above example, R, W, a can be followed by a B, which means that the file is opened in binary form
Return value: The file opens, returning a pointer to the open file (file structure), failure to open files, error code (error codes)
Note: After the fopen operation to be judged, whether the file is open, the file is actually open to do the following read or write operations, if there are errors to be handled incorrectly
Example: FILE *pfile=fopen (const char *filename, "RB");
Open file stream There is also a function that supports wide characters, as follows
FILE *_wfopen (const wchar_t *filename,const wchar_t *mode)
fread
Function prototype: size_t fread (void* buff,size_t size,size_t count,file* Stream)
Function: Reads data from a file into a specified address
Parameters: The first parameter is the pointer to the data (buff), or the address of the data store
The second parameter is the size of a single element, the size of the data that is written to the address by the pointer, and the unit is byte
The third parameter is the number of elements, that is, the element that is the size of the data to be read
The fourth parameter is the file pointer that provides the data, which points to the file's internal data
Return value: Number of total data elements read
Cases:
int num,count;
int* Pr=new Int[num*count];
Fread (PR, num*4, Count, stream); Stream is the file pointer returned in fopen
To write data to PR, you must allocate memory for PR, one int is 4 bytes, so be x4
fseek
Function prototype: int fseek (FILE *stream,long offset,int framewhere)
Role: Reposition a pointer inside a file
Parameters: The first is the file pointer, the second is the pointer offset, and the third is the pointer offset starting position
Return value: Relocation successfully returns 0, otherwise it returns a value other than 0
It is important to note that the function is not to reposition the file pointer, but to reposition the pointer inside the file so that pointers to the internal data in the file are moved to the data in the file that we are interested in, which is primarily the purpose of relocation.
Note: Execution succeeded, then the stream points to the position of offset bytes based on Fromwhere. Execution failed (for example, if offset offset is outside the file size), keep the original stream position unchanged
fclose
Function prototype: int fclose (FILE *stream)
Function: Closes a file stream, using fclose to output the last remaining data in the buffer to the disk file and releasing the file pointer and related buffers
Proficient use of the above four functions can be obtained from the file to our useful data type, the premise for the file format is very understanding, for example, for a DIB bitmap file, you can read out his files in the header and pixel information.