- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在读取这样的文件:
31 0A 34 0A 31 0A 38 0A 34 0A 33 0A 36 0A 31 0A 31 0A 39 0A 31 30 0A 31 30 0A 35 0A 35 0A 31 30 0A 31 0A 33 0A 36 0A 33 0A 31 30 0A 35 0A 31 0A 31 30 0A 39 0A 35 0A 38 0A 33 0A 36 0A 34 0A 33 0A 36 0A 35 0A 31 30 0A 37 0A 32 0A 36 0A 33 0A 36 0A 35 0A 31 30 0A 37 0A 39 0A 33 0A 36 0A 32 0A 36 0A 35 0A 34 0A 0A 30 20 31 20 34 37 32 37 0A 30 20 33 20 36 33 36 33 0A 30 20 34 20 33 36 35 37 0A 30 20 35 20 33 31 33 30 0A 30 20 36 20 32 34 31 34 0A 30
我打算使用以下代码阅读文件的第一部分,直到找到序列 0A 0A:
readed = fscanf(f,"%s", str_aux);
在0A 0A之后我需要阅读以下句子:
readed = fscanf(f,"%s %s %s", str_aux1, str_aux3, str_aux3);
我如何检测 0A 0A 以便开始读取文件的第二部分。
我想使用以下结构:
while (something){
readed = fscanf(f,"%s", str_aux);
}
while (fscanf(f, "%s %s %s", a,b,c)!= EOF){
...
...
}
对某些情况的一些想法(在第一个 while 内)?
我在 Linux 上工作。
最佳答案
我可能会使用 fgetc 和状态引擎。像这样的东西:
int state=0;
char d;
while ((d = fgetc(f)) >= 0) {
if (d==0x0a)
state++;
else
state = 0;
if (state == 2)
do_something();
}
不过,我可能会使用 fgets。像这样的东西:
int state=0;
char line[MAXLINE];
while (fgets(line,sizeof(line),f))
if (state == 0 && *line == 0x0a)
state=1;
else if (state == 1)
{
sscanf(line,"%s %s %s",a,b,c);
do_something_else();
}
}
一般来说,我对调用 fscanf() 非常谨慎。我一直发现 fscanf 解析器非常脆弱。我更有可能通过 fgets 或其他方式获取该行,然后解析结果。
关于c - 使用 C 的 fscanf 检测 0x0a,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171462/
我正在用 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
我是一名优秀的程序员,十分优秀!