簡體   English   中英

以語言 c 讀取文件 txt

[英]read file txt in langage c

這是一個正在進行的 cesar 加密問題是當我使用“openFile”方法打開文件時

/* 自從我接觸 c 語言以來已經有 2 年了,我要回到它 */

我有這個代碼其他文件:

//method to encrypt a char
char encrypt(char c, int jump) {
    int overflow = 0;
    char res = c;
    char tabChars[26] = {'a','b','c','d','e','f','g','h',
                   'i','j','k','l','m','n','o','p','q',
                   'r','s','t','u','v','w','x','y','z'};

    for(int i=0;i<sizeof (tabChars);i++) {
        if(c == tabChars[i]) {
            if(i+jump > 25) {
                overflow = i+jump - 26;
                res = tabChars[overflow];
            } else if(i+jump <= 25) {
                res = tabChars[i+jump];
            }
        }
    }
    return res;
    };

void writeInFile(FILE* file) {
    //todo
};

// error here
void openFile(char* file) {
    FILE* f = fopen(file, "r");
    printf("\nopen file : %s\n", file);
    char c;
    while((c = getc(f)) != EOF) {
        putchar(c);
    }
    fclose(f);
};

和主要代碼:

int jump = 5;
char c = 't';
printf("\nletter change with : --%c--\n",encrypt(c,jump));

char* file = "taget/to/message.txt";
openFile(file); // error here 

但我有一個錯誤:

Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.

為什么不工作? 謝謝大家

with if(f) // 有效,謝謝大家的評論

void openFile(char* file) {
FILE* f = fopen(file, "r");
if(f) {
    printf("\nopen file : %s\n", file);
    char c;
    while((c = getc(f)) != EOF) {
        putchar(c);
    }
    fclose(f);
} else {
    printf("\nopen error...\n");
}
};

出 function:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM