简体   繁体   English

解码文件

[英]Decoding a file

#include <Windows.h>
#include <cstdio>
const int KEY=111;

void encryptStrA(char* sometext)
{
    int length;
    length=strlen(sometext);
    for(int i=0; i<length;i++)
        sometext[i]^=KEY;
}

int main(void)
{   
    FILE* pFile=fopen("pliczek","wb");
    char sign;
    char sampleString[]="Hello world!";

    encryptStrA(sampleString);
    fprintf(pFile,"%c%c%s%c%c",13^KEY,10^KEY,sampleString,13^KEY,10^KEY);
    fclose(pFile);

    pFile=fopen("pliczek","rb");
    while(!feof(pFile))
    {   
        fscanf(pFile,"%c",&sign);
        printf("%c",sign^KEY);
    }

    fclose(pFile);
    system("PAUSE");
    return 0;
}

I evaded some tricky things 我回避了一些棘手的事情

  1. File is opened in binary mode 文件以二进制模式打开
  2. In encryptStrA strlen function isn't placed directly in the loop condition 在encryptStrA中,strlen函数不直接放在循环条件中

In spite of these, it still has been outputting "Hell" instead of "Hello World!"? 尽管如此,它仍然输出“地狱”而不是“Hello World!”? More precisely, cuts everything after spotting the key character .What's the reason? 更确切地说,在发现关键人物后削减一切。这是什么原因? I use OS in which every line of text is ended with carriage return(ASCII 13) and line feed (10). 我使用OS,其中每行文本都以回车符(ASCII 13)和换行符(10)结束。

The code fprintf("%s", s); 代码fprintf("%s", s); expects s to be a zero-terminated string. 期望s是一个以零结尾的字符串。 When you reach 'o'^111 it gives a null character, so the rest of the string is not written to the file. 到达'o'^111它会给出一个空字符,因此字符串的其余部分不会写入文件。

You can use fwrite instead. 你可以改用fwrite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM