在C中读取文件的第二行

时间:2019-04-26 14:54:40

标签: c file fgets

因此,我编写了一个Cesar加密程序,该加密程序用数字将“空格”字符的位置保存在加密文件的第二行中(例如:

  

HMEQSRHLMIV //加密文本(通常在此处输入)
  7 //空格字符所在的数字

) 我现在的问题是,当我用fgets()读取字符串时,它只能读取到\ n,所以如何获取文件的第二行?

这是我的代码,如果有帮助的话。

#include <stdio.h>
#include <stdbool.h>

#define asize 80
void deleteEnter(char[]);
void decrypt(char[], int, char[]);
int main() {
    char Message[asize] = {0};
    int code = 0;
    char encMessageFile[asize + 4] = {};
    char encMessage[asize] = {};
    char MessageFile[asize] = {};
    FILE *fp;
    FILE *fp2;
    bool open = false;
    printf("Gib die verschluesselte Datei an: \t");
    fgets(encMessageFile,asize + 4, stdin);
    deleteEnter(encMessageFile);
    fp = fopen(encMessageFile, "r");
    do {
        if (fp == NULL) {
            printf("\nEs wurde keine Datei gefunden die so heisst\n");
            open = false;
            fclose(fp);
        } else {
            open = true;
        }
    } while (open == false);
    fgets(encMessage,asize,fp);
    printf("\nGib den Code zum entschluesseln ein: \t");
    scanf("%i", &code);
    decrypt(encMessage, code, Message);
    for (int i = 0; i < asize + 4; ++i) {
        if (encMessageFile[i] == '.' && encMessageFile[i + 1] == 'e' && encMessageFile[i + 2] == 'n' && encMessageFile[i + 3]  == 'c' && encMessageFile[i + 4] == '\0') {
            for (int j = 0; j < i; j++) {
                if (encMessageFile[j] != '\0') {
                    MessageFile[j] = encMessageFile[j];
                    MessageFile[j + 1] = '\0';
                }

            }
            i = asize + 4;
        }
    }
    fp2 = fopen(MessageFile, "w");
    fputs(Message, fp2);
    fclose(fp2);
    fclose(fp);
    remove(encMessageFile);



}
void deleteEnter(char encMessage[]){
    for (int i = 0; i < asize; i++) {
        if (encMessage[i] == '\0') {
            i = asize;
        } else if (encMessage[i] == '\n'){
            encMessage[i] = '\0';
            i = asize;
        }
    }
}
void decrypt(char encMessage[], int code, char Message[]) {
    int a = code % 26;
    int LeerPos[asize] = {0};
    for (int k = 0; k < asize; k++) {
        if (encMessage[k] == '\n'){
            for (int i = 0; i < asize; ++i) {
                if (encMessage[k + i] != '\0') {
                    LeerPos[i] = encMessage[k + i] - 80;
                } else {
                    i = asize;
                }
            }
            encMessage[k] = '\0';
        }
    }
    for (int i = 0; i < asize; i++) {
        if (encMessage[i] == '\0') {
            i = asize;
        } else {
            for (int j = 0; j < a; j++) {
                encMessage[i] = encMessage[i] - 1;
                if (encMessage[i] < 'A' && encMessage[i] > 'A' - 2) {
                    encMessage[i] = 'Z';
                }
            }
        }
    }
    int x = 0;
    for (int l = 0; l < asize; l++) {
        for (int i = 0; i < asize; i++) {
            if (l == LeerPos[i]) {
                Message[l] = ' ';
                x++;
            } else {
                Message[l] = encMessage[l - x];
                }
        }
    }
}

我希望解密消息后的程序是“ DIAMOND HIER”

因为我也发布了加密的用户旺旺,所以它是:

#include <stdio.h>
#include <stdbool.h>

#define asize 80

void cleanup (char[],char[], int[]);//2 arrays input und endinput
void deleteEnter(char[]);
void toupper(char[]); //endinput
int laenge(char[]); //input pointeranzleer
void encrypt(char[], int);
void Ausgabe(char[], char[], int[]);
//i abspeichern. for schleife in dritte array und array+1 und i++
int main() {
    FILE *fp;
    int schluessel=0;
    char input[asize]={' '};
    char endinput[asize]={' '};
    char name[asize] = {' '};
    bool open = false;
    int LeerPos[80] = {0};

    do {
        printf("Bitte geben Sie den Namen der Datei ein:");
        fgets(name, asize, stdin);
        deleteEnter(name);
        fp = fopen(name, "r");

        if (fp == NULL) {
            printf("\nEs wurde keine Datei gefunden die so heisst\n");
            open = false;
            fclose(fp);
        } else {
            open = true;
        }
    } while (open == false);

    fgets(input, asize, fp);
    printf("Bitte geben sie den Schluessel ein:");
    scanf("%d",&schluessel);
    cleanup(input,endinput, LeerPos);
    toupper(endinput);
    encrypt(endinput, schluessel);
    Ausgabe(endinput, name, LeerPos);
    fclose(fp);




    return 0;
}
int laenge(char input[asize]){
    int i=0;
    while(input[i]!='\0'){
        i++;
    }
    return i;
}
void cleanup(char input[asize],char endinput[asize], int LeerPos[]){
    int j=0;
    int LC = 0;
    int lengthIn = laenge(input);
    for(int i=0;i<lengthIn;i++){
        if(input[i]!=' '){
            endinput[j]=input[i];
            j++;
        } else {
            LeerPos[LC] = i;
            LC++;
        }

    }
}
void toupper(char endinput[asize]){
    for(int i=0;i<laenge(endinput);i++){
        if(endinput[i]>='a'&&endinput[i]<='z'){
            endinput[i]=endinput[i]-('z'-'Z');
        }
    }
}

void encrypt(char S[asize], int code) {
    int a = code % 26;
    for (int i = 0; i < 80; i++) {
        if (S[i] == '\0') {
            i = 80;
        } else {
            for (int j = 0; j < a; j++) {
                S[i] = S[i]+1;
                if (S[i] == 'Z' + 1) {
                    S[i] = 'A';
                }
            }
        }
    }
}

void Ausgabe(char S[asize], char name[asize], int LeerPos[]) {
    FILE *fpOut;
    char EName[asize + 4];
    char EncEnd[4] = {'.','e','n','c'};

    for (int j = 0; j < asize; j++) {
        EName[j] = name[j];
    }

    for (int k = 0; k < asize + 4; k++) {
        if (EName[k] == '\0'){
            for (int i = k; i < k + 4; ++i) {
                EName[i] = EncEnd[i - k];
            }
            k = asize+4;
        }
    }

    fpOut = fopen(EName, "w");
    printf("\nVerschluesselte File: %s", EName);
    for (int i = 0; i < asize; i++) {
        if (S[i] == '\0') {
            i = asize;
        }else {
            fprintf(fpOut, "%c", S[i]);
        }
    }
    fprintf(fpOut, "%c", '\n');
    for (int l = 0; l < asize; l++) {
        if (LeerPos[l] != '\0'){
            fprintf(fpOut, "%i", LeerPos[l]);
        } else {
            l = asize;
        }
    }
    remove(name);
    fclose(fpOut);
}

void deleteEnter(char S[]) {
    for (int i = 0; i < asize ; i++) {
        if (S[i] == '\0') {
            i = asize;
        } else if (S[i] == '\n') {
            S[i] = '\0';
        }
    }
}

感谢所有帮助。 Arigatou Gozaimasu

0 个答案:

没有答案