Use of fopen, Fread, Fseek, fclose in the file stream

Source: Internet
Author: User
Tags fread

File file stream for fast operation of files, the main operation functions are fopen, fseek, Fread, Fclose, when the structure of the file is relatively clear when using these functions will be relatively quick to get the file location of the data, extract the information useful to us, Meet the needs in programming. The following are explained separately, as well as the precautions they use

fopen

Function prototype FILE * fopen (const char *path,cost char *mode)

Function: Opens a file that returns a pointer to the file

Parameter description: The first parameter is the file path and filename to open the file, and the second parameter indicates how the file is opened

Note: Mode has the following values:

R: Read-only open, file must exist

r+: Readable and writable, must exist

rb+: Open binary file, can read and write

rt+: Open Text file, can read and write

W: Write only, file exists then file length clear 0, file does not exist then create this file

w+: Can read and write, file exists then file length clear 0, file does not exist then establish the file

A: Additional way to open only write, does not exist to establish the file, there is write data added to the end of the file, EOF character reserved

A +: Additional way to open read-write, does not exist to establish the file, there is write data added to the end of the file, EOF characters are not preserved

WB: Open binary file, write only

wb+: Open or create binary file, can read and write

wt+: Open or create a text file to read and write

at+: Open Text file, read/write, write data to the end of text

ab+: Open binaries, read/write, write data to the end of file

It is known from the mode character that the above, such as R, W, and A, can be appended with a B, indicating that the file is opened in binary form

Return value: The file opens, returns a pointer to the open file (file structure), failed to open, error code (error codes)

Note: After the fopen operation to determine whether the file is open, the file is really open for subsequent read or write operations, if there is an error to do error handling

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 the file into the specified address

Parameters: The first parameter is the pointer (buff) that receives the data, which is the address of the data store

The second parameter is the size of a single element, that is, the data size that is written to the address by the pointer, and the unit is byte

The third parameter is the number of elements, which is the element to be read with a size of

The fourth parameter is a file pointer that provides the data, which points to the internal data of the file

Return value: Total number of 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 the PR, you must allocate memory for the PR, an int of 4 bytes, so x4

Fseek

Function prototype: int fseek (FILE *stream,long offset,int framewhere)

Role: Reposition the pointer inside the file

Parameters: The first is a file pointer, the second is the offset of the pointer, and the third is the start position of the pointer offset

Return value: Relocation successfully returned 0, otherwise 0 value not returned

It is important to note that the function is not to relocate the file pointer, but rather to reposition the pointer inside the file, so that pointers to the internal data of the file are moved to the data in the file that we are interested in, and the relocation is primarily for this purpose.

Description: Successful execution, the stream points to a position that offsets the offset byte as a datum of fromwhere. Execution failed (for example, offset offset is out of file size), the location of the original stream remains unchanged

Fclose

Function prototype: int fclose (FILE *stream)

Function: Close a file stream and use Fclose to output the last remaining data in the buffer to the disk file and release the file pointer and the associated buffer

Skilled use of the above four functions can be obtained from the file for 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 information and pixel information.

Transferred from: http://www.cnblogs.com/Romi/archive/2012/02/29/2374769.html

Use of fopen, Fread, Fseek, fclose in the file stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.