, fopen
추가 모드에서 파일을 보내고을, 파일 포인터는 파일의 시작에 처음이다 . 당신이 무언가를 쓰거나 곧 fseek
을 사용하자마자 끝까지 이동합니다.
내 if (!ftell(report_csv))
앞에 fseek(report_csv, 0, SEEK_END);
을 추가해야했습니다.
이것을 확인해 보겠습니다.
코드 :
#include <stdio.h>
int main(int argc, char **argv) {
FILE *test;
size_t size;
char buf[100];
/* Truncate file */
test = fopen("test", "w");
if (!test) {
fprintf(stderr, "Cannot open file `test`!\n");
return 1;
}
/* Write something */
fprintf(test, "Something. ");
fclose(test);
/* Open in append */
test = fopen("test", "a+");
if (!test) {
fprintf(stderr, "Cannot open `test` in append mode!\n");
return 1;
}
/* Try to get the file size */
size = ftell(test);
printf("File pointer is: %d\n", size);
fseek(test, 0, SEEK_END);
size = ftell(test);
printf("After `fseek(test, 0, SEEK_END)`, the file pointer is: %d\n", size);
/* Append */
fprintf(test, "And that. ");
fclose(test);
/* Same without fseek */
test = fopen("test", "a+");
if (!test) {
fprintf(stderr, "Cannot open `test` in append mode!\n");
return 1;
}
fprintf(test, "Hello! ");
size = ftell(test);
printf("File size is now: %d\n", size);
fclose(test);
/* Try to read */
test = fopen("test", "r");
if (!test) {
fprintf(stderr, "Unable to open `test` for reading!\n");
return 1;
}
printf("File contents:\n\t");
while (test && !feof(test)) {
fgets(buf, sizeof(buf), test);
printf("%s", buf);
}
/* Cleanup & exit */
fclose(test);
printf("\n\nExiting.\n");
return 0;
}
출력는 :
File pointer is: 0
After `fseek(test, 0, SEEK_END)`, the file pointer is: 11
File size is now: 28
File contents:
Something. And that. Hello!
Exiting.
파일 입출력 작업은 운영 체제마다 다르므로 작업하는 OS는 무엇입니까? –
'CSV 출력 파일을 열 수 없습니다 ... '는 * bad * 오류 메시지의 표준적인 예입니다. 실제 파일 이름과 오류의 이유를 포함하십시오 ('man perror'와'man strerror'). –
@EitanT : Windows 용 –