Windows 中的二进制输出
我编写了一个程序,它读取二进制文件,对其内容进行一些处理并将结果写入不同的文件。在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 编程的经验,我真的不知道在哪里可能是问题所在。
I wrote a program that reads a binary file, does some process with its contents and writes the results to a different file. In Linux it works perfectly, but in Windows it does not work; the output files are always 1KB...
This is a simplified version of the program:
#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);
}
I would love to give you more details of the problem, but I don't have much experience programming C in Windows and I really don't know where may be the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用
b
标志来 fopen:我知道,由于一些(
脑损伤)主观决定,在 win32 上输入流达到 0x1A 时会触发如果文件不是以“二进制模式”打开,则为 EOF。
编辑
从未研究过它,但现在有人告诉我
0x1A
在DOS中用作软EOF。You should use the
b
flag to fopen:I understand that due to some (
brain-damage) subjective decisions, on win32 reaching 0x1A on the input stream triggers anEOF
if the file is not opened in "binary mode".EDIT
In never looked into it but somebody is telling me now that
0x1A
was used in DOS as a soft EOF.好吧,您不是以二进制模式打开文件(使用“wb”和“rb”)。这在 Linux 上无关紧要,但在 Windows 上却很重要,在文本模式下读/写文件时,Windows 会转换某些字节。例如:
Well, you're not opening the files in binary mode (use "wb" and "rb"). This doesn't matter on Linux, but it does on Windows, which will transform certain bytes when reading/writing a file in text mode. For example:
您需要将“rb”和“wb”与 fopen 一起使用。
You need to use "rb" and "wb" with fopen.