简体   繁体   English

在C的每一行中读取不同格式的文件

[英]Reading file with different format in each line in C

I just start learning C(optional school course). 我刚刚开始学习C(可选的学校课程)。 I stuck on a small problem for 2 days. 我在一个小问题上停留了2天。 So the basic idea is, I have bunch of data in a file that I want to extract. 因此,基本思路是,我要提取的文件中包含大量数据。 However, there are 2 formats that the data has, and the first letter on each line determines what action I need to take. 但是,数据有2种格式,每行的第一个字母决定了我需要采取的措施。

For example, the data in file looks like these: 例如,文件中的数据如下所示:

S:John,engineer,male,30
S:Alice,teacher,female,40
C:Ford Focus,4-door,25000
C:Chevy Corvette,sports,56000
S:Anna,police,female,36

What I want to do is, after open the file, read each line. 我想做的是,打开文件后,阅读每一行。 If the first letter is S, then use 如果第一个字母是S,则使用
fscanf(fp, "%*c:%[^,],%[^,],%[^,],%d%*c",name,job,sex,&age)
to store all variable so I can pass them to function people(). 存储所有变量,以便我可以将它们传递给函数people()。

But if the first letter is C, then use 但是,如果第一个字母是C,则使用
fscanf(fp, "%*c:%[^,],%[^,],%d%*c",car,type,&price)
to store so I can pass them to function vehicle(). 存储,这样我就可以将它们传递给函数vehicle()。

Would really appreciated if anyone can give me some pointer on how to do this. 如果有人可以给我一些指导,我将不胜感激。 Thanks. 谢谢。

There are many approaches, but separating IO from parsing is a good first step. 有很多方法,但是将IO与解析分离是一个很好的第一步。

With line oriented data, it is so much cleaner to simply 使用面向行的数据,只需简单地清洁

FILE *inf = ...;
char buf[100];
if (fgets(buf, sizeof buf, inf) == NULL) Handle_EOForIOError();

Then parse it. 然后解析它。

char name[sizeof buf];
char job[sizeof buf]; 
char sex[sizeof buf]; 
unsigned age;
char car[sizeof buf]; 
char type[sizeof buf]; 
unsigned cost; 
int n;

if (sscanf(buf, "S:%[^,],%[^,],%[^,],%u %n", name, job, sex, &age, &n) == 4 && 
    buf[n] == '\0') 
  Good_SRecord();
else if (sscanf(buf, "C:%[^,],%[^,],%u %n", car, type, &cost &n) == 3 && 
     buf[n] == '\0')  
  Good_CRecord();
else Garbage();

The " %n" trick is really good at making sure all the data parsed as expected without extra junk. " %n"技巧确实非常擅长确保按预期方式解析所有数据,而不会产生多余的垃圾。

Here is the rough idea: parse the string in 2 steps: 这是一个大概的想法:分两个步骤解析字符串:

"%c:%s"

will get you the first character and the rest of the string. 将为您提供第一个字符和字符串的其余部分。 Then, based on what you read in the first character, you can continue parsing the remaining part as 然后,根据您在第一个字符中看到的内容,可以继续将其余部分解析为

"%[^,],%[^,],%[^,],%d%c"

or 要么

"%c:%[^,],%[^,],%d%c"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM