本文介绍了阅读并在中间与EOF指示文件复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用code下面从一个二进制文件复制到另一个,但第一个文件包含了一些EOF指标(0xFF的),作为它的一部分,因此复制函数实际上将文件复制到它的第一个EOF指标。

I used the code below to copy from one binary file to another, but the first file contains some EOF indicators (0xFF) as part of it, so the copy function actually copies the file until its first EOF indicator.

例如:如果我的文件是 {0×01,0×02,×03,为0xFF,0×01,0×02,为0xFF,0xFF的} 则仅 {0×01 ,0×02,×03} 将被复制到新的文件。任何想法如何解决它(或者也许我失去了一些东西有...)

For example: if my file is {0x01, 0x02, 0x03, 0xFF, 0x01, 0x02, 0xFF, 0xFF} then only {0x01, 0x02, 0x03} will be copied to the new file. Any idea how to fix it (or maybe I'm missing something there...)

code:

int Util_Copy_File(char* source, char* dest)
{
    FILE *fs,*ft;  
    char ch;
    char infile[100];
    sprintf(infile, "%s", dest);
    fs = fopen(infile,"r");  
    if(fs==NULL)  
    {
        return -1;  
    }
    ft = fopen(dest,"w");  
    if(ft==NULL)  
    {  
    fclose(fs);  
    return STATUS_FAIL;
    }  

    while(1)  
    {  
    ch = getc(fs);  
    if(ch==EOF)  
    {  
        break;  
    }  
    else  
        putc(ch,ft);  
    } 
    fclose(fs);  
    fclose(ft);
    return 0;
}

谢谢,
本雅明

Thanks,Binyamin

推荐答案

龟etc返回一个int,而不是一个字符,所以你可以告诉EOF并用相同的值EOF一个字符之间的区别。

fgetc returns an int, not a char , so you can tell the difference between EOF and a char with the same value as EOF.

修改

char ch;

int ch

和(通常是不相关的,如果你是在* nix)

And (usually not relevant if you're on *nix)

fs = fopen(infile,"r");  

fs = fopen(infile,"rb");  

这篇关于阅读并在中间与EOF指示文件复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:55