C language file Random Read and write detailed and sample code _c language

Source: Internet
Author: User
Tags constant fread rewind

The file read and write functions described earlier are sequential read and write, that is, read and write files can only start from scratch, read and write the data in turn. But in the actual development of the need to read and write the middle part of the file, to solve this problem, you have to move the file inside the position pointer, and then read and write. This method of reading and writing is called Random Read and write, that is, start from anywhere in the file.

The key to realizing random reading and writing is to move the position pointer as required, which is called file positioning.

File positioning functions Rewind and fseek

There are mainly two functions that move the internal position pointer of a file, namely rewind () and fseek ().

Rewind () is used to move the position pointer to the beginning of the file, which has been used several times before, and its prototype is:

void Rewind (FILE *fp);

Fseek () is used to move the position pointer to any position, and its prototype is:

int fseek (FILE *fp, long offset, int origin);

Parameter description:

1 FP is a file pointer, which is the file being moved.

2 Offset is the number of bytes to be moved. The long type is a larger range that you want to move, and a larger file to handle.

3 origin is the starting position, that is, where to start calculating the offset. The C language has three starting positions, the beginning of the file, the current position and the end of the file, respectively.

Each location is represented by a corresponding Changshilai:

starting point constant Name constant Value
Beginning of File Seek_set 0
Current position Seek_cur 1
End of File Seek_end 2

For example, move the position pointer to 100 bytes from the beginning of the file:

Fseek (FP, 100, 0);

It is worth noting that fseek () is generally used in binary files, where the computed position is sometimes faulted because of the conversion.

Random Read and write of files

After you move the position pointer, you can read and write using any of the read and write functions described earlier. Because it is a binary file, Common fread () and fwrite () are read and write.

The example enters three sets of student information from the keyboard, saves it to a file, and then reads the second student's information.

#include <stdio.h>
#define N 3
struct stu{
  char name[10];//name
  int num;//School number
  int age;
  float score//Results
} Boys[n], boy, *pboys;
int main () {
  FILE *fp;
  int i;
  Pboys = boys;
  if ((Fp=fopen ("D:\\demo.txt", "wb+")) = = NULL) {
    printf ("Cannot open file, press any key to exit!\n");
    Getch ();
    Exit (1);
  }
  printf ("Input data:\n");
  For (i=0 i<n; i++,pboys++) {
    scanf ("%s%d%d%f", Pboys->name, &pboys->num, &pboys->age, & Pboys->score);
  }
  Fwrite (boys, sizeof (struct Stu), N, FP); Write three student information
  fseek (FP, sizeof (struct stu), seek_set);//move position pointer
  fread (&boy, sizeof (struct Stu), 1, FP);// Read a student information
  printf ("%s%d%d%f\n", Boy.name, Boy.num, Boy.age, boy.score);
  Fclose (FP);
  return 0;
}

Run Result:

Input Data:
Tom 2 15 90.5↙
Hua 1 14 99↙
Zhao 10 16 95.5↙
Hua 1 14 99.000000

The above is the  c language files random Read and write data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.