本文介绍了如何从getc读取过去的EOF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个XOR加密程序,该程序在加密期间但在解密期间可以正常工作

I am writing a XOR encryption program which works fine during encryption but during decryptionthe

被卡在一个点上并且在对该问题的最佳猜测是(加密的文件包含各种字符)之后,就没有解密发生了,只要fgetc到达可以在文件实际结尾之前出现的EOF标记它卡在那里并停止阅读下一个字符.

gets stuck at one point and no decryption takes place after that my best guess about the problem is (the encrypted file contains all sorts of characters ) as soon as fgetc reaches EOF mark which can be present before the actual end of the file it gets stuck there and stop reading the next characters .

这是getc()的某种限制吗?这是我的垃圾代码

is this some kind of limitation of getc() ? here is my rubbish code

int get_file_size(char  filename[])
{
    FILE *p_file = NULL;
    p_file = fopen(filename,"rb");
    fseek(p_file,0,SEEK_END);
    int size = ftell(p_file);
    fclose(p_file);
    return size;
}

int endec(char filename[],char psdw[])
{
    FILE *f;
    int hashed=0,ed=0;
    int inphash=inhash(psdw);
    inphash=inphash%50;
    f=fopen(filename,"r");
    if(f==NULL)
    printf("failed");

    char temps[999999];
    long int crs=0,j=0;
    int filesz=get_file_size(filename);
    printf("file size = %d\n\n",filesz);
while(1){

    inphash=inphash+2;
    char ca=(char)inphash;
    char ca2=fgetc(f);
    printf("%c\n",ca2);
    if(crs>=filesz)
        break;
    temps[crs]= ca2 ^ ca;
    crs++;

        }
fclose(f);
printf("%d",strlen(temps));

FILE *fp;
fp=fopen(filename,"wt");
for(j=0;j<crs;j++){
        putc (temps[j] , fp);
printf("%c",temps[j]);

}
fclose(fp);
}

推荐答案

您的问题就在这里:

f=fopen(filename,"r");

您打开文件用于文本阅读,而不是二进制文件.您的文件大小功能可以正确处理,但解码器功能却不能.

You open the file for text reading, not for binary. Your file size function gets it right, but your decoder function does not.

使用C风格的IO例程逐个字符地读取文件的惯用方式是这样的:

The idiomatic way to read a file character by character using the C-style IO routines is like this:

f = fopen(filename, "rb");

if (!f)
    // handle error

int c;   // NOTE:  int, not char!

while ( (c = fgetc(f)) != EOF )
{
    // do something with 'c'
}

这个习惯用法不是要求您将文件大小作为一个单独的操作来获取.您可以使用上述形式的简单循环来重写XOR加密"例程.它将更加清晰和简洁.

This idiom does not require you to get the file size as a separate operation. You can rewrite your XOR "encryption" routine with a simple loop of the above form. It will be much clearer and more concise.

您的整个解码器功能可以重写如下:(减去调试代码)

Your entire decoder function could be rewritten as follows: (minus the debug code)

int endec(char filename[], char psdw[])
{
     int inphash = inhash(psdw) % 50;
     char temp[999999];  // really, should be std::vector<char>
     FILE *f;

     if ( (f = fopen(filename, "rb")) == NULL )
     {
         printf("opening for read failed\n");
         return -1;
     }

     size_t crs = 0;
     int    c;

     while ( (c = fgetc(f)) != EOF )
     {
         inphash += 2;
         temp[crs++] = (char)(inphash ^ c);
     }
     fclose(f);

     if ( (f = fopen(filename, "wt")) == NULL )
     {
         printf("opening for write failed\n");
         return -1;
     }

     if (fwrite(temp, crs, 1, f) != crs)
     {
         printf("short write\n");
         fclose(f);
         return -1;
     }

     fclose(f);
     return 0;
 }

不是 stellar 错误处理,但它是错误处理.

Not stellar error handling, but it is error handling.

这篇关于如何从getc读取过去的EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:51