嘿社区,我在从命令行复制二进制文件时遇到问题,看看我的尝试。下面的代码复制了 srcFile 上的第一个字符并以某种方式停止,你们能帮我弄清楚解决这个问题的方法以及它发生的原因吗?
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 8500
int main(int argc, char** argv)
{
char buffer[BUFSIZE];
size_t currBit;
FILE *srcFile = fopen(argv[1], "r+b");
if (!srcFile)
{
printf("source file doesnt exist or problem with allocating the memory");
return 1;
}
FILE *dstFile = fopen(argv[2], "r+b");
if (dstFile)
{
printf("Destination file already exists\nfor overwriting enter 1 \nfor exit enter 2\n");
_flushall();
switch (getchar())
{
case '1':
fclose(dstFile);
dstFile = fopen(argv[2], "w+b");
if (!dstFile)
{
printf("Problem with allocating the memory");
}
while ((currBit = fread(buffer, sizeof(char),BUFSIZE, srcFile) > 0))
{
fwrite(buffer, sizeof(char), currBit, dstFile);
}
break;
case '2':
system("pause");
return 0;
break;
default:
printf("next time you should enter numbers as following");
}
}
else
{
dstFile = fopen(argv[2], "w+b");
while ((currBit = fread(buffer, sizeof(char), BUFSIZE, srcFile) > 0))
{
fwrite(buffer, sizeof(char), currBit, dstFile);
}
if (!dstFile)
{
printf("Problem with allocating the memory");
}
}
fclose(srcFile);
fclose(dstFile);
getchar();
return 0;
}
输入源文件: 10100101
目标文件的预期输出: 10100101
目标文件输出: 1
最佳答案
while ((currBit = fread(buffer, sizeof(char),BUFSIZE, srcFile) > 0))
看看>
在哪里...在分配给currBit
之前与零进行比较,因此您总是分配一个 bool 值值(1/0)。将 >
移动到括号的最外层。
关于c - 在 C 中复制二进制文件的神秘问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37087430/