簡體   English   中英

當我讀入這個文件並掃描一個int時,為什么輸出的數字真的很大?

[英]How come when I read in this file and scan in an int, the number that is outputted is REALLY large?

我必須要做家庭作業,但是我想知道為什么會這樣:

int main(int argc, char* argv[])
{
char* name;
printf("Please enter the name of the file: \n");
scanf("%s", &name);
FILE * input;
input = fopen("input1.txt", "w");
if(input == NULL)
{
    printf("This file does not exist");
}
else
{
char* line = (char*)malloc(sizeof(char));
int infixOrPostfix;
int i = 0, j;
}


while(fscanf(input, "%s", line[i]) != '0')
{
    printf("%d", infixOrPostfix);
    if(infixOrPostfix == 1)
    {
        fscanf(input, "%s", &line);
        evaluateInfix(line);
    }
    else if(infixOrPostfix == 2)
    {
        fscanf(input, "%s", line);
        printf("%s", line);
        evaluatePostfix(line);
    }
    i++;
}
fclose(input);
free(line);
}

當我使用print語句運行此零件時,它會打印出一個非常大的數字,而我希望可以打印的數字是2。我在做什么錯? 謝謝您的回答,我現在可以告訴您,我不太喜歡缺乏c語言的技能。

 char * name;

用。。。來代替

 char name[255];

並閱讀有關靜態和動態內存分配的內容。


 char* line = (char*)malloc(sizeof(char));

這是錯誤的,我讓您找出原因。

暫無
暫無

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

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