gpt4 book ai didi

C - 从文本文件获取单个 Int

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

我有一个如下所示的文本文件:

4
3
Samantha Octavia Ivan Henry
100 90 65 70
99 50 70 88
88 90 98 100

我只想单独阅读前两行并将其打印出来,但就目前情况而言,它给了我一个巨大的数字。

inputFile = fopen ("input.txt", "r");

//input
if( inputFile == NULL)
{
printf ("Unable to open file input.txt");
exit(EXIT_FAILURE);
}
else
{
printf ("How many students?\n ");
fscanf (inputFile, "%d", &students);
printf ("%d", &students);
printf ("\nHow many assignments?\n ");
fscanf (inputFile, "%d", &assignments);
printf ("%d", &assignments);
printf ("\n");
}

我在这里缺少什么?

最佳答案

简单错误!

打印 &students 或 &assignments 的值不正确。这将打印指向该变量的指针的值。您需要以下代码:

    printf ("How many students?\n ");
fscanf (inputFile, "%d", &students);
printf ("%d", students); // not &students
printf ("\nHow many assignments?\n ");
fscanf (inputFile, "%d", &assignments);
printf ("%d", assignments); // not &assignments
printf ("\n");

关于C - 从文本文件获取单个 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759477/

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