Windows中的二进制输出 [英] Binary output in Windows

查看:203
本文介绍了Windows中的二进制输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序,读取一个二进制文件,对其内容进行一些处理,并将结果写入一个不同的文件。在Linux中它完美的工作,但在Windows中,它不工作;输出文件总是1KB ...

这是程序的简化版本:

pre > #include< stdio.h>

void copyFile(char * source,char * dest);
$ b int main(int argc,char * argv [])
{
if(argc!= 3)
printf(usage:%s< source> ;< destination>,argv [0]);
else
{
copyFile(argv [1],argv [2]);



$ b 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\\\
);
return;


if(destinationFile == 0)
{
printf(Could not open destination file\\\
);
return;
}

//获取文件大小
fseek(sourceFile,0,SEEK_END); //找到文件的最后
if(ftell(sourceFile)< 4)
return; //返回文件小于4字节
fseek(sourceFile,0,SEEK_SET); //回到开头

fseek(sourceFile,0,SEEK_SET); //回到开头

int currentChar; ((currentChar = fgetc(sourceFile))!= EOF)


{
fputc(currentChar,destinationFile);
}

fclose(sourceFile);
fclose(destinationFile);
}

我很乐意给你提供这个问题的更多细节,在Windows下编程C有很多经验,我真的不知道在哪里可能会遇到这个问题。 解决方案

b flag to fopen:

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

据我所知,由于一些(主要是脑部损伤)主观的决定,如果文件没有以二进制模式打开,则在输入流上的win32上达到0x1A触发一个 EOF



编辑



从来没有看过,但现在有人告诉我, 0x1A 在DOS中用作一个软EOF。


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.

解决方案

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.

这篇关于Windows中的二进制输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆