C Files I / O: Mở, đọc, ghi và đóng tệp

Trong hướng dẫn này, bạn sẽ tìm hiểu về cách xử lý tệp trong C. Bạn sẽ học cách xử lý I / O tiêu chuẩn trong C bằng cách sử dụng fprintf (), fscanf (), fread (), fwrite (), fseek (), v.v. với sự trợ giúp của các ví dụ.

Tệp là một vùng chứa trong các thiết bị lưu trữ máy tính được sử dụng để lưu trữ dữ liệu.

Tại sao cần có tệp?

  • Khi một chương trình bị kết thúc, toàn bộ dữ liệu sẽ bị mất. Lưu trữ trong một tệp sẽ bảo toàn dữ liệu của bạn ngay cả khi chương trình kết thúc.
  • Nếu bạn phải nhập một số lượng lớn dữ liệu, bạn sẽ mất rất nhiều thời gian để nhập hết chúng.
    Tuy nhiên, nếu bạn có một tệp chứa tất cả dữ liệu, bạn có thể dễ dàng truy cập nội dung của tệp bằng một vài lệnh trong C.
  • Bạn có thể dễ dàng di chuyển dữ liệu của mình từ máy tính này sang máy tính khác mà không có bất kỳ thay đổi nào.

Các loại tệp

Khi xử lý tệp, có hai loại tệp bạn nên biết:

  1. Tệp văn bản
  2. Tệp nhị phân

1. Tệp văn bản

Tệp văn bản là tệp .txt bình thường . Bạn có thể dễ dàng tạo các tệp văn bản bằng bất kỳ trình soạn thảo văn bản đơn giản nào như Notepad.

Khi bạn mở các tệp đó, bạn sẽ thấy tất cả nội dung trong tệp dưới dạng văn bản thuần túy. Bạn có thể dễ dàng chỉnh sửa hoặc xóa nội dung.

Chúng cần nỗ lực tối thiểu để duy trì, dễ đọc và cung cấp ít bảo mật nhất và chiếm không gian lưu trữ lớn hơn.

2. Tệp nhị phân

Các tệp nhị phân chủ yếu là tệp .bin trong máy tính của bạn.

Thay vì lưu trữ dữ liệu ở dạng văn bản thuần túy, họ lưu trữ dữ liệu ở dạng nhị phân (0 và 1).

Chúng có thể chứa một lượng dữ liệu cao hơn, không thể đọc được dễ dàng và cung cấp bảo mật tốt hơn các tệp văn bản.

Thao tác tệp

Trong C, bạn có thể thực hiện bốn thao tác chính trên tệp, văn bản hoặc nhị phân:

  1. Tạo một tệp mới
  2. Mở một tệp hiện có
  3. Đóng tệp
  4. Đọc và ghi thông tin vào tệp

Làm việc với tệp

Khi làm việc với tệp, bạn cần khai báo một con trỏ kiểu tệp. Khai báo này là cần thiết để giao tiếp giữa tệp và chương trình.

 FILE *fptr;

Mở tệp - để tạo và chỉnh sửa

Mở tệp được thực hiện bằng cách sử dụng fopen()chức năng được xác định trong stdio.htệp tiêu đề.

Cú pháp để mở tệp trong I / O tiêu chuẩn là:

 ptr = fopen("fileopen","mode"); 

Ví dụ,

 fopen("E:\cprogram\newprogram.txt","w"); fopen("E:\cprogram\oldprogram.bin","rb");
  • Giả sử tệp newprogram.txtkhông tồn tại ở vị trí E:cprogram. Hàm đầu tiên tạo một tệp mới có tên newprogram.txtvà mở tệp đó để viết theo chế độ 'w' .
    Chế độ ghi cho phép bạn tạo và chỉnh sửa (ghi đè) nội dung của tệp.
  • Bây giờ, hãy giả sử tệp nhị phân thứ hai oldprogram.bintồn tại trong vị trí E:cprogram. Hàm thứ hai mở tệp hiện có để đọc ở chế độ nhị phân 'rb' .
    Chế độ đọc chỉ cho phép bạn đọc tệp, bạn không thể ghi vào tệp.
Chế độ mở trong I / O tiêu chuẩn
Chế độ Ý nghĩa của Chế độ Trong thời gian không có tệp
r Mở để đọc. Nếu tệp không tồn tại, fopen()trả về NULL.
rb Open for reading in binary mode. If the file does not exist, fopen() returns NULL.
w Open for writing. If the file exists, its contents are overwritten.
If the file does not exist, it will be created.
wb Open for writing in binary mode. If the file exists, its contents are overwritten.
If the file does not exist, it will be created.
a Open for append.
Data is added to the end of the file.
If the file does not exist, it will be created.
ab Open for append in binary mode.
Data is added to the end of the file.
If the file does not exist, it will be created.
r+ Open for both reading and writing. If the file does not exist, fopen() returns NULL.
rb+ Open for both reading and writing in binary mode. If the file does not exist, fopen() returns NULL.
w+ Open for both reading and writing. If the file exists, its contents are overwritten.
If the file does not exist, it will be created.
wb+ Open for both reading and writing in binary mode. If the file exists, its contents are overwritten.
If the file does not exist, it will be created.
a+ Open for both reading and appending. If the file does not exist, it will be created.
ab+ Open for both reading and appending in binary mode. If the file does not exist, it will be created.

Closing a File

The file (both text and binary) should be closed after reading/writing.

Closing a file is performed using the fclose() function.

 fclose(fptr);

Here, fptr is a file pointer associated with the file to be closed.

Reading and writing to a text file

For reading and writing to a text file, we use the functions fprintf() and fscanf().

They are just the file versions of printf() and scanf(). The only difference is that fprint() and fscanf() expects a pointer to the structure FILE.

Example 1: Write to a text file

 #include #include int main() ( int num; FILE *fptr; // use appropriate location if you are using MacOS or Linux fptr = fopen("C:\program.txt","w"); if(fptr == NULL) ( printf("Error!"); exit(1); ) printf("Enter num: "); scanf("%d",&num); fprintf(fptr,"%d",num); fclose(fptr); return 0; ) 

This program takes a number from the user and stores in the file program.txt.

After you compile and run this program, you can see a text file program.txt created in C drive of your computer. When you open the file, you can see the integer you entered.

Example 2: Read from a text file

 #include #include int main() ( int num; FILE *fptr; if ((fptr = fopen("C:\program.txt","r")) == NULL)( printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); ) fscanf(fptr,"%d", &num); printf("Value of n=%d", num); fclose(fptr); return 0; ) 

This program reads the integer present in the program.txt file and prints it onto the screen.

If you successfully created the file from Example 1, running this program will get you the integer you entered.

Other functions like fgetchar(), fputc() etc. can be used in a similar way.

Reading and writing to a binary file

Functions fread() and fwrite() are used for reading from and writing to a file on the disk respectively in case of binary files.

Writing to a binary file

To write into a binary file, you need to use the fwrite() function. The functions take four arguments:

  1. address of data to be written in the disk
  2. size of data to be written in the disk
  3. number of such type of data
  4. pointer to the file where you want to write.
 fwrite(addressData, sizeData, numbersData, pointerToFile);

Example 3: Write to a binary file using fwrite()

 #include #include struct threeNum ( int n1, n2, n3; ); int main() ( int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:\program.bin","wb")) == NULL)( printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); ) for(n = 1; n < 5; ++n) ( num.n1 = n; num.n2 = 5*n; num.n3 = 5*n + 1; fwrite(&num, sizeof(struct threeNum), 1, fptr); ) fclose(fptr); return 0; ) 

In this program, we create a new file program.bin in the C drive.

We declare a structure threeNum with three numbers - n1, n2 and n3, and define it in the main function as num.

Now, inside the for loop, we store the value into the file using fwrite().

The first parameter takes the address of num and the second parameter takes the size of the structure threeNum.

Since we're only inserting one instance of num, the third parameter is 1. And, the last parameter *fptr points to the file we're storing the data.

Finally, we close the file.

Reading from a binary file

Function fread() also take 4 arguments similar to the fwrite() function as above.

 fread(addressData, sizeData, numbersData, pointerToFile);

Example 4: Read from a binary file using fread()

 #include #include struct threeNum ( int n1, n2, n3; ); int main() ( int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:\program.bin","rb")) == NULL)( printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); ) for(n = 1; n < 5; ++n) ( fread(&num, sizeof(struct threeNum), 1, fptr); printf("n1: %d n2: %d n3: %d", num.n1, num.n2, num.n3); ) fclose(fptr); return 0; ) 

In this program, you read the same file program.bin and loop through the records one by one.

In simple terms, you read one threeNum record of threeNum size from the file pointed by *fptr into the structure num.

You'll get the same records you inserted in Example 3.

Getting data using fseek()

If you have many records inside a file and need to access a record at a specific position, you need to loop through all the records before it to get the record.

This will waste a lot of memory and operation time. An easier way to get to the required data can be achieved using fseek().

As the name suggests, fseek() seeks the cursor to the given record in the file.

Syntax of fseek()

 fseek(FILE * stream, long int offset, int whence);

The first parameter stream is the pointer to the file. The second parameter is the position of the record to be found, and the third parameter specifies the location where the offset starts.

Các thời điểm khác nhau trong fseek ()
Khi nào Ý nghĩa
SEEK_SET Bắt đầu offset từ đầu tệp.
SEEK_END Bắt đầu phần bù từ cuối tệp.
SEEK_CUR Bắt đầu độ lệch từ vị trí hiện tại của con trỏ trong tệp.

Ví dụ 5: fseek ()

 #include #include struct threeNum ( int n1, n2, n3; ); int main() ( int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:\program.bin","rb")) == NULL)( printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); ) // Moves the cursor to the end of the file fseek(fptr, -sizeof(struct threeNum), SEEK_END); for(n = 1; n < 5; ++n) ( fread(&num, sizeof(struct threeNum), 1, fptr); printf("n1: %d n2: %d n3: %d", num.n1, num.n2, num.n3); fseek(fptr, -2*sizeof(struct threeNum), SEEK_CUR); ) fclose(fptr); return 0; ) 

Chương trình này sẽ bắt đầu đọc các bản ghi từ tệp program.bintheo thứ tự ngược lại (từ cuối đến trước) và in ra.

thú vị bài viết...