我编写了一个程序,它读取二进制文件,对其内容进行一些处理并将结果写入不同的文件。在Linux中它可以完美运行,但在Windows中它不起作用;输出文件始终为 1KB...
这是该程序的简化版本:
#include <stdio.h>
void copyFile(char* source, char* dest);
int main (int argc, char* argv[])
{
if (argc != 3)
printf ("usage: %s <source> <destination>", argv[0]);
else
{
copyFile(argv[1], argv[2]);
}
}
void encryptFile(char* source, char* destination)
{
FILE *sourceFile;
FILE *destinationFile;
int fileSize;
sourceFile = fopen(source, "r");
destinationFile = fopen(destination, "w");
if (sourceFile == 0)
{
printf ("Could not open source file\n");
return;
}
if (destinationFile == 0)
{
printf ("Could not open destination file\n");
return;
}
// Get file size
fseek(sourceFile, 0, SEEK_END); // Seek to the end of the file
if (ftell(sourceFile) < 4)
return; // Return if the file is less than 4 bytes
fseek(sourceFile, 0, SEEK_SET); // Seek back to the beginning
fseek(sourceFile, 0, SEEK_SET); // Seek back to the beginning
int currentChar;
while ((currentChar = fgetc(sourceFile)) != EOF)
{
fputc(currentChar, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
}
我很乐意为您提供有关该问题的更多详细信息,但我没有太多在 Windows 中进行 C 编程的经验,而且我真的不知道问题出在哪里。
您应该使用b
标记为 fopen:
fopen(source, "rb")
fopen(destination, "wb");
我知道,由于一些(脑损伤)主观决定,在 win32 上输入流达到 0x1A 时会触发EOF
如果文件不是以“二进制模式”打开。
EDIT
从未研究过,但现在有人告诉我0x1A
在 DOS 中用作软 EOF。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)