Computer >> Máy Tính >  >> Lập trình >> lập trình C

Đọc / ghi cấu trúc vào tệp bằng C

fwrite () và fread () được sử dụng để ghi vào một tệp trong C.

Cú pháp

fwrite ()

fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)

ở đâu

ptr - Một con trỏ tới mảng các phần tử sẽ được viết

kích thước - Kích thước tính bằng byte của mỗi phần tử sẽ được viết

nmemb - Số phần tử, mỗi phần tử có kích thước byte

luồng - Một con trỏ đến một đối tượng FILE chỉ định một luồng đầu ra

Cú pháp

fread ()

fread(void *ptr, size_t size, size_t nmemb, FILE *stream)

ở đâu

ptr - Một con trỏ đến một khối bộ nhớ có kích thước tối thiểu là size * nmemb byte.

size - Kích thước tính bằng byte của mỗi phần tử sẽ được đọc.

nmemb - Số phần tử, mỗi phần tử có kích thước byte.

luồng - Một con trỏ đến một đối tượng FILE chỉ định một luồng đầu vào.

Thuật toán

Begin
   Create a structure Student to declare variables.
   Open file to write.
   Check if any error occurs in file opening.
   Initialize the variables with data.
   If file open successfully, write struct using write method.
      Close the file for writing.
   Open the file to read.
   Check if any error occurs in file opening.
   If file open successfully, read the file using read method.
      Close the file for reading.
   Check if any error occurs.
   Print the data.
End.

Đây là một ví dụ để đọc / ghi cấu trúc trong C:

Mã mẫu

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
   int roll_no;
   char name[20];
};
int main () {
   FILE *of;
   of= fopen ("c1.txt", "w");
   if (of == NULL) {
      fprintf(stderr, "\nError to open the file\n");
      exit (1);
   }
   struct Student inp1 = {1, "Ram"};
   struct Student inp2 = {2, "Shyam"};
   fwrite (&inp1, sizeof(struct Student), 1, of);
   fwrite (&inp2, sizeof(struct Student), 1, of);
   if(fwrite != 0)
      printf("Contents to file written successfully !\n");
   else
      printf("Error writing file !\n");
   fclose (of);
   FILE *inf;
   struct Student inp;
   inf = fopen ("c1.txt", "r");
   if (inf == NULL) {
      fprintf(stderr, "\nError to open the file\n");
      exit (1);
   }
   while(fread(&inp, sizeof(struct Student), 1, inf))
      printf ("roll_no = %d name = %s\n", inp.roll_no, inp.name);
   fclose (inf);
}

Bỏ qua

Contents to file written successfully !
roll_no = 1 name = Ram
roll_no = 2 name = Shyam