Các lỗi thường gặp khi đọc file eof năm 2024

1. fread

include*

include*

// Cú pháp: sizet fread[ void *ptr, sizet size, sizet count, FILE *stream]; // ptr − Đây là con trỏ tới một khối bộ nhớ với kích cỡ tối thiểu là [size*count] byte. // size − Đây là kích cỡ [giá trị byte] của mỗi phần tử được đọc. // count − Đây là số phần tử, với mỗi phần tử có kích cỡ là size byte. // stream − Đây là con trỏ tới một đối tượng FILE mà xác định một Input Stream. // Hàm này trả về số phần tử đã đọc được với giá trị kiểu sizet. FILE pFile; long sizeFile; char buffer; size_t result; string nameFile = "data.txt"; pFile = **fopen[nameFile.data[], "r"]; if[pFile == nullptr]{

  fprintf[stderr, "Error: %s\n", **strerror**[**errno**]];  
  **exit**[**EXIT_FAILURE**];  
} // Di chuyển con trỏ về vị trí 0 bắt đầu tại cuối file. fseek[pFile, 0, SEEK_END]; // Trả về vị trí hiện tại của con trỏ đang đứng. sizeFile = ftell[pFile]; // Di chuyển con trỏ về vị trí đầu tiên của file. rewind[pFile]; fprintf[stdout, "size file: %ld byte \n", sizeFile]; // Cấp phát vùng nhớ cho con trỏ buffer. buffer = [char*] malloc [sizeof[char] *sizeFile]; if[buffer == nullptr] {
  fprintf[stderr, "Error: %s \n", **strerror**[**errno**]];  
  **exit**[**EXIT_FAILURE**];  
} result = fread[buffer, 1, sizeFile, pFile]; fprintf[stdout, "fread: %ld byte \n", result]; if[[long]result != sizeFile]{
  fprintf[stderr, "Error: %s \n", **strerror**[**errno**]];  
  **exit**[**EXIT_FAILURE**];  
}else{
  fprintf[stdout, "buffer: %s \n", **buffer**];  
} fclose[pFile]; free[buffer]; return EXIT_SUCCESS;

2. fopen

include*

// Cú pháp: FILE *fopen[const char *filename, const char *mode]; // Hàm này trả về con trỏ FILE nếu không sẽ trả về con trỏ nullptr. // một số mode tham khảo // [r] mở file ra để đọc, file này phải tồn tại trước. // [w] mở file ra để ghi, file sẽ được tạo nếu chưa tồn tại, ngược lại sẽ xóa nội dung của file. // [a] mở file ra để ghi, file sẽ được tạo nếu chưa tồn tại, ngược lại sẽ thêm nội dung vào cuối file. // [r+] mở file ra để đọc và ghi, file này phải tồn tại trước. // [w+] mở file ra để ghi và đọc, file sẽ được tạo nếu chưa tồn tại, ngược lại sẽ xóa nội dung của file. // [a+] mở file ra để ghi và đọc, file sẽ được tạo nếu chưa tồn tại, ngược lại sẽ thêm nội dung vào cuối file. // có thêm option b cho trường hợp đọc và ghi dữ liệu cho file nhị phân như: rb, wb, ab, rb+, wb+, ab+. FILE pFile; pFile = *fopen["data.txt","w"]; if[pFile != nullptr]{         fputs["Nguyen Van A", pFile];         fclose[pFile];         return EXIT_SUCCESS; } return EXIT_FAILURE;

3. fwrite

include*

// Cú pháp: sizet fwrite[const void *ptr, sizet size, sizet count, FILE *stream]; // Tham số // ptr − Đây là con trỏ tới mảng các phần tử được ghi. // size − Đây là kích cỡ [giá trị byte] của mỗi phần tử được ghi. // count − Đây là số phần tử, với mỗi phần tử có kích cỡ là size byte. // stream − Đây là con trỏ tới một đối tượng FILE mà xác định một Output Stream. // Trả về giá trị // Hàm này trả về tổng số phần tử được trả về thành công dưới dạng một đối tượng sizet. // Nếu số này khác tham số count thì có lỗi xảy ra. FILE pFile; char buffer[] = "Nguyen Van B"; *pFile = fopen["data.bin", "wb"]; size_t num = fwrite[buffer , sizeof[char], sizeof[buffer], pFile]; fclose[pFile]; if[num != sizeof[buffer]]{         printf["fwrite error \n"];         return EXIT_FAILURE; } return EXIT_SUCCESS;

4. fseek

include*

// Cú pháp: int fseek[FILE *stream, long int offset, int origin]; // Hàm này dùng để đặt con trỏ đến vị trí offset. // origin có 3 giá trị: SEEKSET, SEEKCUR và SEEKEND. // SEEKSET: vị trí đầu file. // SEEKCUR: vị trí hiện tại của con trỏ file. // SEEKEND: vị trí cuối file. FILE pFile; pFile = *fopen["data.txt", "wb"]; fputs["This is an apple.", pFile]; fseek[pFile, 9, SEEK_SET]; fputs[" sam", pFile]; // This is a sample. fclose[pFile];

5. ftell

include*

// Cú pháp: long int ftell[FILE *stream]; // Hàm này trả về vị trí con trỏ hiện tại của stream. FILE pFile; long size; pFile = *fopen["data.txt","rb"]; if[pFile == nullptr]{       perror["Error opening file"]; }else{       fseek[pFile, 0, SEEK_END];       size = ftell[pFile];       fclose[pFile];       printf["Size of data.txt: %ld bytes.\n", size]; }

6. rewind

include*

// Cú pháp: void rewind [FILE *stream]; // Hàm này sẽ di chuyển con trỏ về đầu file. int n; FILE pFile; char buffer [27]; pFile = *fopen["data.txt","w+"]; for[n = 'A'; n = 0 là thành công, ngược lại là lỗi. FILE pFile; char text[256]; printf["Enter string to append: "]; *fgets[text, 256, stdin]; pFile = fopen["data.txt","a"];

int num = fputs[text, pFile]; if[num >= 0]{     printf["fputs success \n"]; }else{   printf["fputs error: %s \n", strerror[errno]]; } fclose[pFile];

8. fgets

include*

// Cú pháp: char *fgets[char *str, int num, FILE *stream]; // Hàm này dùng để đọc chuỗi num ký tự hoặc gặp ký tự kết thúc chuỗi trước. // Hàm trả về con trỏ str nếu thành công, ngược lại trả con trỏ nullptr. FILE pFile; char text[256]; pFile = *fopen["data.txt", "r"]; if[pFile == nullptr]{     perror["Error opening file"]; // Câu trên tương đương với câu: printf["Error opening file: %s \n", strerror[errno]]; }else{     if[fgets[text, 256, pFile] != nullptr]{         puts[text];      }else{

           printf["fgets error: %s \n", **strerror**[**errno**]];  
   }  
    fclose[pFile]; }

9. fgetc

include*

// Cú pháp: int fgetc[FILE *stream]; // Hàm này dùng để đọc 1 ký tự trong FILE, và trả về EOF khi kết thúc. FILE pFile; int c; pFile= *fopen["data.txt","r"]; if[pFile == nullptr]{       perror["Error opening file"]; }else{       do{         c = fgetc[pFile];       printf["%c \n", c];

  }while[**c** != **EOF**];  
      fclose[pFile]; }

10. fputc

include*

// Cú pháp: int fputc[int character, FILE *stream]; // Hàm này dùng để ghi 1 ký tự vào FILE. FILE pFile; char c; pFile = *fopen["data.txt","w"]; if[pFile != nullptr]{       for[c = 'A'; c

Chủ Đề