为什么还没有到达文件末尾时会收到 EOF?
Why am I getting EOF when the end of the file is not reached yet?
我正在 Windows 上制作一个 C 程序,它制作另一个文件的精确副本。当我在我自己制作的包含 8192 'A' 个字符的文件或任何文本文件上使用它时,它工作得很好,但当我尝试复制 .exe 本身或 .png 图像时,它却无法正常工作
我总是在我要复制的文件中的相同位置(通常是 4096)得到 EOF(即使当我比较 EOF 位置和文件大小时显然还没有到达文件末尾)。
这是我制作的程序:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
int main(int argc, char **argv){
// open src file and get its size
if(argc != 2)
return 1;
FILE *src = fopen(argv[1], "r");
if(src == NULL)
return 2;
fseek(src, 0, SEEK_END);
int file_size = ftell(src);
rewind(src);
// make destination file
FILE *dst = fopen("copy", "w");
// copy content of src in dst 1 byte at a time
uint8_t byte = 0;
for(int i = 0; i < file_size; i++){
// if eof or whatever for whatever reason
if(fread(&byte, sizeof(uint8_t), 1, src) != 1){
/*
should I move the cursor myself if I get EOF?
i mean the copy is already trash so it doesn't matter am i right c
*/
byte = 0;
}
fwrite(&byte, sizeof(uint8_t), 1, dst);
}
// usually says 4096
printf("%ld\n", ftell(src));
fclose(dst);
fclose(src);
return 0;
}
现在我知道“fread() != 1”并不一定意味着 EOF 但是当我在“if(fread(...) != 0) 中测试”feof(src) != 0“时" 事实上是这样。
所以我的问题是,“为什么我在要复制的文件结束之前得到 EOF?”,以及“我将如何在 C 中复制文件?”
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
int main(int argc, char **argv){
// open src file and get its size
if(argc != 3) // <<-- HERE
return 1;
FILE *src = fopen(argv[1], "rb"); // <<-- HERE
if(src == NULL)
return 2;
fseek(src, 0, SEEK_END);
int file_size = ftell(src);
rewind(src);
// make destination file
FILE *dst = fopen("copy", "wb"); // <<-- HERE
// copy content of src in dst 1 byte at a time
uint8_t byte = 0;
for(int i = 0; i < file_size; i++){
// if eof or whatever for whatever reason
if(fread(&byte, sizeof(uint8_t), 1, src) != 1){
/*
should I move the cursor myself if I get EOF?
i mean the copy is already trash so it doesn't matter am i right c
*/
byte = 0;
}
fwrite(&byte, sizeof(uint8_t), 1, dst);
}
// usually says 4096
printf("%ld\n", ftell(src));
fclose(dst);
fclose(src);
return 0;
}
我正在 Windows 上制作一个 C 程序,它制作另一个文件的精确副本。当我在我自己制作的包含 8192 'A' 个字符的文件或任何文本文件上使用它时,它工作得很好,但当我尝试复制 .exe 本身或 .png 图像时,它却无法正常工作 我总是在我要复制的文件中的相同位置(通常是 4096)得到 EOF(即使当我比较 EOF 位置和文件大小时显然还没有到达文件末尾)。
这是我制作的程序:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
int main(int argc, char **argv){
// open src file and get its size
if(argc != 2)
return 1;
FILE *src = fopen(argv[1], "r");
if(src == NULL)
return 2;
fseek(src, 0, SEEK_END);
int file_size = ftell(src);
rewind(src);
// make destination file
FILE *dst = fopen("copy", "w");
// copy content of src in dst 1 byte at a time
uint8_t byte = 0;
for(int i = 0; i < file_size; i++){
// if eof or whatever for whatever reason
if(fread(&byte, sizeof(uint8_t), 1, src) != 1){
/*
should I move the cursor myself if I get EOF?
i mean the copy is already trash so it doesn't matter am i right c
*/
byte = 0;
}
fwrite(&byte, sizeof(uint8_t), 1, dst);
}
// usually says 4096
printf("%ld\n", ftell(src));
fclose(dst);
fclose(src);
return 0;
}
现在我知道“fread() != 1”并不一定意味着 EOF 但是当我在“if(fread(...) != 0) 中测试”feof(src) != 0“时" 事实上是这样。
所以我的问题是,“为什么我在要复制的文件结束之前得到 EOF?”,以及“我将如何在 C 中复制文件?”
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
int main(int argc, char **argv){
// open src file and get its size
if(argc != 3) // <<-- HERE
return 1;
FILE *src = fopen(argv[1], "rb"); // <<-- HERE
if(src == NULL)
return 2;
fseek(src, 0, SEEK_END);
int file_size = ftell(src);
rewind(src);
// make destination file
FILE *dst = fopen("copy", "wb"); // <<-- HERE
// copy content of src in dst 1 byte at a time
uint8_t byte = 0;
for(int i = 0; i < file_size; i++){
// if eof or whatever for whatever reason
if(fread(&byte, sizeof(uint8_t), 1, src) != 1){
/*
should I move the cursor myself if I get EOF?
i mean the copy is already trash so it doesn't matter am i right c
*/
byte = 0;
}
fwrite(&byte, sizeof(uint8_t), 1, dst);
}
// usually says 4096
printf("%ld\n", ftell(src));
fclose(dst);
fclose(src);
return 0;
}