簡體   English   中英

getc() 在主 function 之外不起作用

[英]getc() not working outside of main function

我寫了一個小程序,它使用 XTEA 密碼算法來加密或解密給定的文本文件。 在我開始整理我的程序之前,一切都很好。

這是曾經工作的代碼:

int main(int argc, char *argv[]){
    char* filename = malloc(20*sizeof(uint8_t));
    uint32_t* argBuf = malloc(2*sizeof(uint32_t)); /* used to buffer (de)cypher strings */
    uint32_t* keyBuf = malloc(4*sizeof(uint32_t));      /* used to store the cypher key */
    int character, i, opt, filelen;
    FILE *finp, *fdec, *fenc;
    i = 0;
    /* get plaintext input file */
    printf("Please enter the following filenames. They\r\n");
    printf("shall be no longer than 100 characters.\r\n");
    printf("Input filename: \r\n");
    fflush(stdin);
    gets(filename);
    fflush(stdin);
    finp = fopen(filename, "r"); 
    if (finp == NULL){
        printf("Error! Input file not found!\r\n");
        return;
    }
.
. /* opening the other file(s) and other stuff not related to problem */
.

    /* iterate over file */ 
    while ((character = getc(finp)) != EOF){
        i++;
        *(((uint8_t*)argBuf) + i) = (uint8_t) character;
        if (++i == 8){
            /* encode */
            printf("\r\nnon ciphered text: ");
            printText((uint64_t*)argBuf);
            printf("\r\nciphered text: ");
            encipher(NUM_CYCLES, argBuf, keyBuf);
            storeCipherText((uint64_t*)argBuf, fenc);
            printText((uint64_t*)argBuf);
            i = 0;
            argBuf[0] = 0;
            argBuf[1] = 0;
        }
    }

}

Output(只是證明文件可以讀取):

***************************************************
*              Cipher programm using XTEA         *
***************************************************

Please enter the following filenames. They
shall be no longer than 20 characters.
Input filename:
t.txt
Cipher text output file: 
aaaa
Deciphered text output file: 
t2.txt

non ciphered text: Hall
ciphered text: *j|+A
.
.
.

現在我寫了一個助手 function 來處理文件打開和讀取。 它包含在名為“HandleEncipher”的 function 中:

void HandleEncipher(void){
char* filename = malloc(20*sizeof(uint8_t));
uint32_t* argBuf = malloc(2*sizeof(uint32_t)); /* used to buffer (de)cypher strings */
uint32_t* keyBuf = malloc(4*sizeof(uint32_t));     /* used to store the cypher key */
int character, i;
FILE *finp, *foutp;

/* get plaintext input file */
printf("Please enter the following filenames. They\r\n");
printf("shall be no longer than 100 characters.\r\n");
printf("Input filename: \r\n");
fflush(stdin);
gets(filename);
fflush(stdin);
finp = fopen(filename, "r"); 
if (finp == NULL){
    printf("Error! Input file not found!\r\n");
    return;
}

/* get cipher text output file */
printf("Cipher text output file: \r\n");
gets(filename);
foutp = fopen(filename, "wb");                 /* create or clear file */
if (foutp == NULL){
    printf("Error! Can't write to output file!\r\n");
    return;
}

StoreCipherKey(keyBuf);

argBuf[0] = 0;                          /* initialize buffer and index */
argBuf[1] = 0;
i = 0;

printf("start iterating");
/* iterate over file */ 
while ((character = fgetc(finp)) != EOF){
    i++;
    printf("character#: %i", i);
    *(((uint8_t*)argBuf) + i) = (uint8_t) character;
    if (++i == 8){
        /* encode */
        printf("\r\nnon ciphered text: ");
        printText((uint64_t*)argBuf);
        printf("\r\nciphered text: ");
        printText((uint64_t*)argBuf);
        storeCipherText((uint64_t*)argBuf, foutp);
        i = 0;
        argBuf[0] = 0;
        argBuf[1] = 0;
    }
}
}

另一個有趣的細節是,如果我在 Visual Studio Code 中調試代碼,如果使用“HandleEncipher”function,我的 finp 變量等於 NULL。

我很困惑,因為外包代碼實際上是在 main.js 中運行良好的代碼的副本。 我不知道可能出了什么問題(除了輸入字符串的邊界檢查不佳)。 但是我在第二個版本中使用了與第一個版本完全相同的文件名,如果我對不存在的文件使用文件名,我的程序會做出相應的反應。

有人知道這里發生了什么嗎?

StoreCipherKey Function:

void StoreCipherKey(uint32_t keyBuffer[4]){
    uint8_t i;
    /* fill key into buffer */
    for (i = 0; i < KEYSIZE/(2*sizeof(*keyBuffer)); i++){
        if (i < KEYSIZE/(sizeof(*keyBuffer)*2)){
            keyBuffer[i] = (uint32_t) TEST_KEY_LOWER >> (i*32);
        } else {
            keyBuffer[i-KEYSIZE/(sizeof(*keyBuffer)*2)]  = (uint32_t) TEST_KEY_UPPER >> ((i - 2*sizeof(*keyBuffer))*32);
        }
    } 
}

在這里,我超越了 keyBuffer。 這會導致未定義的行為。 是的,在我當前的時區,編程為時已晚。

感謝@Joshua 暗示了這個問題。

暫無
暫無

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

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