在vi

时间:2015-10-07 15:56:15

标签: c encryption

我已经厌倦了调试和更改我的代码,但每次它都给我:

No newline at end of file

当我尝试:

diff main.c output.txt

我的程序对输入文本文件进行加密/解密,并在转换时提供正确的输出但是当我输入diff main.c output.txt时,它总是会指向我的错误。我该怎么做才能防止这种情况发生?任何帮助将不胜感激!

int encryptFile(FILE *input, FILE *output){
    char c;
    char p;
    int r = 0;
    char p1 = 0;
    char c1 = 0;
    int index = 0;
    char line[100];
   // while((p = fgetc(input)) != EOF){
      while(fgets(line, sizeof(line), input) != NULL){
        for(index = 0; line[index] != 0; index++){
        p = line[index];
        r = rand() % 97;

        if(p == 't'){
            p1 = 0;
        }
        else if(p == '\n'){
            p1 = 1;
        }
        else{
            p1 = p - 30;
        }

        c1 = p1 ^ r;
        if(c1 == 0){
            c = 't';
        }
        else if(c1 == 1){
            c = '\n';
        }
        else{
            c = c1 + 30;
        }
        //Write
        fputc(c, output);
    }
      }
    return 1;
}

1 个答案:

答案 0 :(得分:1)

尝试在output文件末尾添加新行:

int encryptFile(FILE *input, FILE *output)
{
    char c = 0;
    char p;
    int r = 0;
    char p1 = 0;
    char c1 = 0;
    int index = 0;
    char line[100];

    while (fgets(line, sizeof(line), input) != NULL) {
        for (index = 0; line[index] != 0; index++) {
            p = line[index];
            r = rand() % 97;
            //change all displayable characters [0...96]
            if (p == 't') {
                p1 = 0;
            } else if (p == '\n') {
                p1 = 1;
            } else {
                p1 = p - 30;
            }
            c1 = p1 ^ r;//bitwise xor
            if (c1 == 0) {
                c = 't';
            } else if (c1 == 1) {
                c = '\n';
            } else {
                c = c1 + 30;
            }
            //Write
            fputc(c, output);
        }
    }
    if (c != '\n') {
        fputc('\n', output);
    }
    return 1;
}

或使用:

diff --ignore-all-space file1 file2