Windows 中的二进制输出

发布于 2024-11-18 17:07:19 字数 1398 浏览 1 评论 0原文

我编写了一个程序,它读取二进制文件,对其内容进行一些处理并将结果写入不同的文件。在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始终不够爱げ你 2024-11-25 17:07:19

您应该使用 b 标志来 fopen:

fopen(source, "rb")
fopen(destination, "wb");

我知道,由于一些(脑损伤)主观决定,在 win32 上输入流达到 0x1A 时会触发 如果文件不是以“二进制模式”打开,则为 EOF。

编辑

从未研究过它,但现在有人告诉我0x1A在DOS中用作软EOF。

You should use the b flag to fopen:

fopen(source, "rb")
fopen(destination, "wb");

I understand that due to some (brain-damage) subjective decisions, on win32 reaching 0x1A on the input stream triggers an EOF 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.

那些过往 2024-11-25 17:07:19

好吧,您不是以二进制模式打开文件(使用“wb”和“rb”)。这在 Linux 上无关紧要,但在 Windows 上却很重要,在文本模式下读/写文件时,Windows 会转换某些字节。例如:

\r\n <--> \n 

\x1a  (Ctrl-Z) is treated as an EOF indicator

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:

\r\n <--> \n 

\x1a  (Ctrl-Z) is treated as an EOF indicator
甜`诱少女 2024-11-25 17:07:19

您需要将“rb”和“wb”与 fopen 一起使用。

You need to use "rb" and "wb" with fopen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文