gpt4 book ai didi

c - 为什么当我读取这个文件并扫描 int 时,输出的数字非常大?

转载 作者:行者123 更新时间:2023-11-30 17:03:10 25 4
gpt4 key购买 nike

我必须为家庭作业执行此操作,但我想知道为什么这种情况发生在我身上:

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));

这是错误的,我会让你找出原因。

关于c - 为什么当我读取这个文件并扫描 int 时,输出的数字非常大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36255645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com