Analysis of VC ++ file operation functions

Source: Internet
Author: User

Ftell:Merit: Returns the current FILE location, that is, the current position of the FILE pointer.

Function prototype: Long ftell (FILE * stream ); Function: The ftell () function is used to obtain the number of offset bytes from the current position of the file position pointer to the first part of the file. When a file is accessed in a random way, the program cannot easily determine the current position of the file because the file location is frequently moved before and after. Using the fseek function and then calling the function ftell () can easily determine the current position of the file. Call example:Ftell (fp); Using the ftell () function, you can easily know the length of a file. For example, the following statement sequence: fseek (fp,
0L, SEEK_END); len = ftell (fp); first, move the current position of the file to the end of the file, and then call the ftell () function to obtain the displacement of the current position relative to the first part of the file, the displacement value is equal to the number of bytes contained in the file.

Fseek:

Benefits:Internal position pointer of a file on a relocated stream (data stream/file)

Note: The file pointer does not point to the file/stream. The position Pointer Points to the byte location inside the file. As the file reads, the file pointer will not change to another file if it is not assigned a value.

Usage:
Int fseek (FILE * stream, long offset, int fromwhere );
  Description: The function sets the position of the file pointer stream. If the execution is successful, stream points to the position of fromwhere (starting position of the offset: File Header 0, current position 1, end 2) as the benchmark, and offset (pointer offset) bytes. If the execution fails ( For example The offset value exceeds the file size.

The fseek function is similar to the lseek function, but lseek returns an off_t value, while fseek returns an integer.

Return Value: If the call succeeds, 0 is returned. If the call fails,-1 is returned, and the errno value is set. You can use the perror () function to output an error.

Fseek position the file position (position) pointer (pointer) for the file referenced by stream to the byte location calculated by offset.

 

 

Fileno: Function Name:Fileno (_ fileno in VC ++ 6.0) Function prototype:Int _ fileno (FILE * stream ); Function:Fileno () is used to obtain the object stream specified by the parameter stream. File descriptor. ( File descriptorIs a handle represented by an unsigned integer, which is used by a process to identify the opened file. The file descriptor is associated with a file object that includes relevant information (such as the file opening mode, file location type, and initial file type), which is called the file context .) Return Value:File handle of a data stream Header file:Stdio. h Related functions:Open, fopen, fclose filelength: Merit: The number of bytes in the file length. Usage:Long filelength (int handle ); Example: FILE * l_file; int L = filelength (fileno (l_file); fread: Merit: Read data from a stream

  Function prototype:
Size_t fread (void *
Buffer, Size_tSize, Size_tCount, FILE *Stream); 

  Parameter:

1. Address (pointer) used to receive data )(Buffer)

2. size of a single element: the unit is byte rather than bit. For example, reading an int type data is 4 bytes.

3. Number of elements (Count)

4. Provide the data file pointer (stream)

  Return Value: Number of read Elements

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.