- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个 .txt 文件,如下所示:
** Paris ** Flight,5 days,visiting various monuments. | 2999.99 |
** Amsterdam ** By bus,7 days, local art gallery. | 999.99 |
** London ** Flight,3 days,lots of free time. | 1499.99 |
我想获取存储在 3 个变量中的信息,城市、描述和价格,但我根本无法保存这些行。
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char city[256],desciption[256];
float temp;
if(fp=fopen("Ponuda.txt","rt")==NULL){
printf("ERROR\n");
exit(1);
}
while(fscanf(fp,"** %s ** %s | %f |",city,description,&temp)==3){
printf("** %s ** %s |%f|\n",city,description,temp);
}
return 0;
最佳答案
IMO 使用fgets
读取每一行文件要容易得多,然后使用strtok
用分隔符字符串"*|"< 分割每一行
.
然后我使用 strdup
将文本字符串复制到结构中,并使用 sscanf
从第三个标记中提取票价。我应该测试 strdup
的返回值,因为它在内部调用了 malloc
。
我还使用 double
而不是 float
(我只会在有约束的情况下使用它)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10
typedef struct {
char *city;
char *descrip;
double fare;
} flight_t;
int main()
{
FILE *fp;
flight_t visit[MAX] = {0};
char buffer [1024];
char *tok;
int records = 0, i;
if((fp = fopen("Ponuda.txt", "rt")) == NULL) {
printf("Error opening file\n");
exit(1);
}
while(fgets(buffer, sizeof buffer, fp) != NULL) {
if((tok = strtok(buffer, "|*")) == NULL) {
break;
}
if(records >= MAX) {
printf("Too many records\n");
exit(1);
}
visit[records].city = strdup(tok); // add NULL error checking
if((tok = strtok(NULL, "|*")) == NULL) { // pass NULL this time
break;
}
visit[records].descrip = strdup(tok); // add NULL error checking
if((tok = strtok(NULL, "|*")) == NULL) { // pass NULL this time
break;
}
if(sscanf(tok, "%lf", &visit[records].fare) != 1) { // read a double
break;
}
records++;
}
fclose(fp);
// print the records
for(i = 0; i < records; i++) {
printf("** %s ** %s |%.2f|\n", visit[i].city, visit[i].descrip, visit[i].fare);
}
// free the memory given by strdup
for(i = 0; i < records; i++) {
free(visit[i].city);
free(visit[i].descrip);
}
return 0;
}
程序输出:
** Paris ** Flight,5 days,visiting various monuments. |2999.990000|
** Amsterdam ** By bus,7 days, local art gallery. |999.990000|
** London ** Flight,3 days,lots of free time. |1499.990000|
关于c - Fscanf 读取 c 中 float 和字符的混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38379915/
我正在用 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
我是一名优秀的程序员,十分优秀!