3. Random reading and writing of files
Sometimes the user wants to read the information directly in the middle of the file, it is obviously inconvenient to read and write in the file in order to start from the file header until the required file location. The Turbo C2.0 provides a random read-write function of a set of files, that is, the position pointer can be positioned directly to read and write in the place where the file is requested.
The random Read and write functions of the file are as follows:
int fseek (FILE *stream, long offset, int fromwhere);
int fread (void *buf, int size, int count, FILE *stream);
int fwrite (void *buf, int size, int count, FILE *stream);
Long Ftell (FILE *stream);
The role of the fseek () function is to set the position pointer of the file to the position of offset bytes starting at Fromwhere, where Fromwhere is one of several macro definitions:
File position pointer start calculation position Fromwhere
━━━━━━━━━━━━━━━━━━━━━━━━━━━
Symbolic constant numeric meaning
───────────────────────────
Seek_set 0 from the beginning of the file
Seek_cur 1 from the current position of the file pointer
Seek_end 2 from the end of the file
━━━━━━━━━━━━━━━━━━━━━━━━━━━
Offset is the number of bytes skipped by the file position pointer from the specified start position (where Fromwhere indicates). It is a long integer to support files larger than 64K bytes. The fseek () function is typically used to manipulate binary files.
When the fseek () function returns 0 o'clock to indicate that the operation was successful, a return of 0 does not represent a failure. The following program reads the 8th byte from the binary file Test_b.dat.
Example 13:
#include <stdio.h>
Main ()
{
FILE *FP;
if ((Fp=fopen ("Test_b.dat", "RB")) ==null)
{
printf ("Can ' t Open file");
Exit (1);
}
Fseek (FP, 8.1, Seek_set);
FGETC (FP);
Fclose (FP);
}
The Fread () function reads count fields from a file, each field length is size byte, and holds them in the buffer that the BUF pointer refers to.
The fwrite () function writes the count field of size bytes in the buffer that the BUF pointer refers to to the file to which the stream points.
As the number of read and write sections increases, so does the file position indicator, the number of bytes read, and the number of bytes that the file position indicator skips accordingly. Read-write function returns the number of fields that are read and written.
The Ftell () function returns the current value of the file position indicator, which is the number of bytes that the indicator starts at the beginning of the file, and the number returned is a long integer, indicating an error when returning-1.
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.