- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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;
}
最佳答案
您提供的代码中有几个问题,我想在得到您要求的答案之前先讨论一下。
fscanf(ptr," %d ",a);
这是错误的。这里是 int
的地址需要作为第三个参数。您需要 & 运算符 &
访问变量的地址,例如:
fscanf(ptr," %d ",&a);
fscanf(ptr," %s ",buf);
也是假的。指向 char
的指针这里需要数组作为第三个参数,但是 buf
在
char* buf[100];
您需要声明buf
以正确的方式作为 char
数组,例如:
char buf[100];
使其适用于:
fscanf(ptr," %s ",buf);
您忘记了#
在 stdio.h
的 include 指令中:
include<stdio.h>
此外,#include
之间应该有一个空格。以及您要包含的文件。
所以预处理器指令应该是这样的:
#include <stdio.h>
如果打开流操作失败,则不应使用 return
值为 0
.
如果操作失败,这对程序本身至关重要,程序的返回值应该是非零值(最常见的是1
的值)或EXIT_FAILURE
(这是为此目的而设计的宏(在 header <stdlib.h>
中定义)),而不是 0
,这向外部软件应用程序以及操作系统表明出现了问题,程序无法按照其原始目的成功执行。
所以使用:
if (ptr==NULL)
{
printf("no such file.");
return 1;
}
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/
我正在用 C 编写一个函数,它应该从文件中输入有关学生及其成绩的信息。但是,有时在运行程序时,即使输出符合预期,测试也会在 fscanf 处显示分析器错误。 /*structure for grade
如何在 fscanf() 中跳过一个值并转到下一个值?例如,我的输入文件中有以下数据: 11112222 3.95 4 22.5 我应该怎么做才能扫描第二个值? (我想跳过11112222扫描3.95
我正在使用这个 for 循环从纬度和经度坐标文件中引入数据,然后将坐标转换为十进制度数,将度数转换为弧度,然后计算分离角度,并返回两个城市之间的距离。我需要比较文件的前两行,计算距离,打印结果,然后再
我在 while(fscanf != EOF) 循环中执行了一些代码。但是,即使 fscanf 已完成执行,我也需要继续运行该代码,直到满足某些条件。我的意思是我想我可以将代码复制/粘贴到 while
我对 C 语言的文件处理非常陌生,我想问几个问题! 我经常使用 fscanf/fget 将文件转换为不同的格式。不过我不太清楚 fscanf 和 fgets 之间的区别。 特别是,我不确定 fscan
我是 C 的新手,这里有这个简单的代码: int main(int argc, char **argv){ FILE *input = stdin; char string[20];
我已经有了一些使用 fscanf() 读取文本文件的代码,现在我需要修改它,以便以前无空格的字段需要允许空格。文本文件基本上是这样的形式: title: DATA title: DATA etc...
我目前正在开发一个简单的 C 应用程序。它采用单个文件作为命令行参数,其格式如下: 1,2,3 4,5,6 7,8,9 etc. 但是,无论出于何种原因,fscanf 都不会扫描数字!这是一个例子:
我正在尝试读取逗号分隔值格式的文件,程序读取了前两种数据类型,但无法读取最后一种。请帮我一些忙 这是我正在尝试读取的文件中的内容: Jane,50,400.60 代码如下: FILE* fpt
我试图让我的程序从文件中读取输入,但不跳过 fscanf 通常执行的空行。扫描文件的文件和循环是: inFile = fopen("text.txt", "r"); for(i = 0; fsca
我正在尝试从 C 文件中读取数据,该文件将始终以以下类型的行进行格式化: 16 Oct 2013 00:01:00.000,0.000,0.000000 这是一个字符串、一个逗号、一个浮点数、一个逗号
如何格式化 fscanf 来格式化输入{'name surname', 'username', 指向不包含撇号的字符串 fscanf(fp,"{%s %s %d}",name,username,use
我正在尝试使用 fscanf 将文件中的行读取到指向字符数组的指针中。我在打印时遇到段错误。我究竟做错了什么?我应该使用 fscanf 以外的函数吗? #include #include #inc
我有一个制表符分隔文件,我正在尝试将其转换为制表符分隔文件。我正在使用 C。我在尝试读取文件的第二行时遇到了困难。现在我只有数万行重复第一行。 #include #include #define
我正在尝试使用 c 读取一个文件,其中每个变量都由竖线字符分隔。我尝试了以下 fscanf(fp, "%s[|], %s[|], \n", str1, str2); 括号之间的字符是垂直线:竖线字符。
我真的卡在某事上了。 我有一个文本文件,其中有 1 个单词后跟 ~100 个 float 。 float 由空格、制表符或换行符分隔。这种格式在整个文本文件中重复多次。 例如,这是文本文件的样子: o
我想使用 fscanf (使用 gcc 的 C 代码)从文件中解析 ip。所以,我想做: char myip[INET_ADDRSTRLEN]; fscanf(file, "%16s", myip);
while 循环不起作用,我不明白为什么。它没有给我任何错误,但它没有读取任何输入内容。文件内容为: 4 $11$ pelle $2$ pollo $333$ palla $41$ alla 我的代码
我有一些代码,我试图从文件中读取一个集团实例。实例文件中的第一行和第二行分别表示顶点和边的数量。但是我的代码似乎没有正确阅读。这是我的代码中与问题相关的部分: int num_edges=0; int
我正在尝试使用 fscanf 解析一个文本 (CSS) 文件并提取所有匹配此模式的语句: @import "some/file/somewhere.css"; 为此,我设置了以下循环: FILE *f
我是一名优秀的程序员,十分优秀!