gpt4 book ai didi

c - 我在从文件中获取数字时遇到问题。函数 fscanf 只读取字符串。 8

转载 作者:行者123 更新时间:2023-11-30 18:23:15 26 4
gpt4 key购买 nike

Fscanf 不起作用,我不明白为什么。它只读取字符串,不读取其他内容。file.txt中写入1 2 3。代码如下:

include<stdio.h> 

int main()
{
FILE* ptr = fopen("file.txt","r");

if (ptr==NULL)
{
printf("no such file.");
return 0;
}

char* buf[100];
int a;

fscanf(ptr," %d ",a);
printf("%d\n", a);
fscanf(ptr," %s ",buf);

printf("%s\n", buf);



return 0;
}

最佳答案

您提供的代码中有几个问题,我想在得到您要求的答案之前先讨论一下。

1.

fscanf(ptr," %d ",a);

这是错误的。这里是 int 的地址需要作为第三个参数。您需要 & 运算符 &访问变量的地址,例如:

fscanf(ptr," %d ",&a);

2.

fscanf(ptr," %s ",buf);

也是假的。指向 char 的指针这里需要数组作为第三个参数,但是 buf

之后被声明为一个包含 100 个指针的数组
char* buf[100]; 

您需要声明buf以正确的方式作为 char数组,例如:

char buf[100]; 

使其适用于:

fscanf(ptr," %s ",buf);

3.

您忘记了#stdio.h 的 include 指令中:

include<stdio.h> 

此外,#include 之间应该有一个空格。以及您要包含的文件。

所以预处理器指令应该是这样的:

#include <stdio.h>

4.

如果打开流操作失败,则不应使用 return值为 0 .

如果操作失败,这对程序本身至关重要,程序的返回值应该是非零值(最常见的是1的值)或EXIT_FAILURE (这是为此目的而设计的宏(在 header <stdlib.h> 中定义)),而不是 0 ,这向外部软件应用程序以及操作系统表明出现了问题,程序无法按照其原始目的成功执行。

所以使用:

if (ptr==NULL) 
{
printf("no such file.");
return 1;
}

5.

Fscanf does not work and I can't understand why. It only reads strings and nothing else.

您期望的结果是什么?你想要什么fscanf()应该做什么?

格式说明符%s用于读取字符串,直到输入流中第一次出现空白字符(跳过前导空白字符,直到遇到匹配序列),由 ptr 指向.

<小时/>

然后我从你的标题中得到:

I have problems with getting numbers from the file

您只需要文件中的数字。

如果您只想从文本文件中获取数字,则不需要 char数组 buf以及读取字符串的全部内容。

只需使用类似的东西:

int a,b,c;            // buffer integers.
fscanf(ptr,"%d %d %d",&a,&b,&c);

printf("%d %d %d\n",a,b,c);

当然,这个表达式意味着它仅适用于 1 2 3 的给定示例。数据或任何相当于(integer) (integer) (integer)的东西但我认为您已经了解它是如何工作的。

当然,您可以使用fscanf()来应用扫描操作。 (以及通过使用 printf() 进行的打印操作)在循环中单独的每个整数,而不是只需一次调用 fscanf() 即可扫描/打印所有整数和printf() ,f.e.像:

#define Integers_in_File 3

int array[Integers_in_File];

for(int i = 0; i < Integers_in_File; i++)
{
fscanf(ptr,"%d",&array[i]); // writing to respective int buffers,
} // provided as elements of an int array.

for(int i = 0; i < Integers_in_File; i++)
{
printf("%d",array[i]); // Output the scanned integers from
} // the file.
<小时/>

整个程序将是:

#include <stdio.h> 

int main()
{
FILE* ptr = fopen("file.txt","r");

if (ptr==NULL)
{
printf("no such file.");
return 1;
}

int a,b,c; // buffer integers.
fscanf(ptr,"%d %d %d",&a,&b,&c);

printf("%d %d %d\n",a,b,c);

return 0;
}

或者:

#include <stdio.h>

#define Integers_in_File 3

int main()
{
int array[Integers_in_File];

FILE* ptr = fopen("file.txt","r");

if (ptr==NULL)
{
printf("no such file.");
return 1;
}

for(int i = 0; i < Integers_in_File; i++)
{
fscanf(ptr," %d",&array[i]); // writing to respective intbuffers,
} // provided as elements of an int
// array.

for(int i = 0; i < Integers_in_File; i++)
{
printf("%d",array[i]); // Output the scanned integers from
} // the file.

return 0;
}

关于c - 我在从文件中获取数字时遇到问题。函数 fscanf 只读取字符串。 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59424939/

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