我是C的新手,并且正在尝试编写一个程序来复制文件,以便我可以学习文件的基础知识.我的代码将文件作为输入,通过使用fseek和ftell从其末尾减去其开始来计算其长度.然后,它使用fwrite根据我从其手册页得到的内容,将一个数据元素(END - START)元素长写入OUT指向的流,从FI给出的位置获取它们.问题是,虽然它确实产生"复制输出",但文件与原始文件不同.我究竟做错了什么?我尝试将输入文件读入变量然后从那里写入,但这也没有帮助.我究竟做错了什么?谢谢
int main(int argc, char* argv[])
{
FILE* fi = fopen(argv[1], "r"); //create the input file for reading
if (fi == NULL)
return 1; // check file exists
int start = ftell(fi); // get file start address
fseek(fi, 0, SEEK_END); // go to end of file
int end = ftell(fi); // get file end address
rewind(fi); // go back to file beginning
FILE* out = fopen("copy output", "w"); // create the output file for writing
fwrite(fi,end-start,1,out); // write …Run Code Online (Sandbox Code Playgroud)