简体   繁体   English

使用fscanf多个值读取C

[英]Reading in C using fscanf multiple values

From a text file of the following format: 来自以下格式的文本文件:

Name Surname ID Address
Name Surname ID Address
Name Surname ID Address

I need to extract each one and put it in a variable. 我需要提取每个变量并将其放入变量中。

I tried the following two methods: 我尝试了以下两种方法:

while(fscanf(fp, "%s,%s,%d,%s\n", name2,surname2,&id2,address2) != EOF){
    printf("Name: %s, Surname: %s, ID: %d, Address: %s\n",
        &name2, &surname2, &id2, &address2);
}

Which gives me the wrong results it only reads one word for each line so the result is something like this: 这给了我错误的结果,它每行只读取一个单词,所以结果是这样的:

Name: Name Surname:@,a ID:2272012 Address: ?"
Name: Surname Surname:@,a ID:2272012 Address: ?"
Name: ID Surname:@,a ID:2272012 Address: ?"
Name: Address Surname:@,a ID:2272012 Address: ?"

And the other method I used was: 我使用的另一种方法是:

while(fscanf(fp, "%s,%s,%d,%s\n", name2,surname2,&id2,address2) == 4){
    printf("Name: %s, Surname: %s, ID: %d, Address: %s\n",
        &name2, &surname2, &id2, &address2);
}

Which reads nothing and nothing happens. 它什么也没读,什么也没发生。

The file I'm reading from is .dat and it's for an assignment and so can only use .dat. 我正在读取的文件是.dat,用于分配作业,因此只能使用.dat。

  • You shouldn't have commas in the format string unless there are commas in the data, which you don't show. 除非数据中没有逗号,否则您不应该在格式字符串中使用逗号。
  • If any of the fields contain spaces, this won't work since %s will stop. 如果任何字段包含空格,则此操作将无效,因为%s将停止。
  • You need to show more of your input .dat file. 您需要显示更多输入的.dat文件。
  • It's often better to read a whole line using fgets() , and then worry about parsing it. 通常最好使用fgets()阅读整行, 然后再担心解析它。

You state that your text file is in the format: 您声明文本文件的格式为:

string<space>string<space>numeric<space>string<newline>

If that is accurate your format string has to match exactly: 如果正确,则格式字符串必须完全匹配:

strings
 |  |  numeric
 |  |  |  +-----string
 |  |  |  | +---newline
 V  V  V  V V
"%s %s %d %s\n"
   ^  ^  ^
   |  |  |
   spaces

This assums each string field is a single block of ASCII values, no spaces/tabs etc. Your code is missing how the variables are defined, presumably it's something like: 假设每个string字段都是一个ASCII值块,没有空格/制表符等。您的代码缺少定义变量的方式,大概是这样的:

char name2[100] = {0};
char surname2[100] = {0};
int id2 = 0;
char address2[100] = {0};

In which case you don't need the & before the names of these in your fscanf string. 在这种情况下,您不需要在fscanf字符串中使用&开头。 So given an input file of: 因此,给定一个输入文件:

firt ast 1234 somebody@something.com 首先发射1234 somebody@something.com
sec but 22352 goo@gle.com 秒,但22352 goo@gle.com
thir haha 9999 nice@lly.com 他们哈哈9999 nice@lly.com

And the above variable declairactions, the following code: 以及上面的变量declairactions,下面的代码:

FILE * fp;
fp = fopen("test.txt", "r");
while(fscanf(fp, "%s %s %d %s\n", name2, surname2, &id2, address2) == 4) 
    printf("name: %s %s, id: %d, address %s\n", name2, surname2, id2, address2);

fclose(fp);

Correctly reads/displays the values. 正确读取/显示值。

First, remove the & from variables in the printf 首先,从printf变量中删除&

printf("Name: %s, Surname: %s, ID: %d, Address: %s\n",
        name2, surname2, id2, address2);

If the separoter between field in your input txt file is comma , 如果您输入的txt文件中的字段之间的分隔符是逗号,

Name,Surname , ID ,Address
Name ,Surname,ID,Address
Name,Surname,ID,Address

Then use the following string format in the fscanf 然后在fscanf使用以下字符串格式

while(fscanf(fp, " %[^, ] , %[^, ] , %d , %[^\n]", name2,surname2,&id2,address2) != EOF){
    printf("Name: %s, Surname: %s, ID: %d, Address: %s\n",
        name2, surname2, id2, address2);
}

If the separoter between fields in your input txt file is space 如果输入txt文件中字段之间的分隔符为空格

Name Surname ID Address
Name Surname ID Address
Name Surname ID Address

Then use the following string format in the fscanf 然后在fscanf使用以下字符串格式

while(fscanf(fp, " %s %s %d %[^\n]", name2,surname2,&id2,address2) != EOF){
    printf("Name: %s, Surname: %s, ID: %d, Address: %s\n",
        name2, surname2, id2, address2);
}
while(fscanf(fp, "%s,%s,%d,%s\n", name2,surname2,&id2,address2) != EOF){
    printf("Name: %s, Surname: %s, ID: %d, Address: %s\n",
        &name2, &surname2, &id2, &address2);
}

I think this is where the error lies: 我认为这是错误所在:

it should be: 它应该是:

while(fscanf(fp, "%s,%s,%d,%s\n", name2,surname2,&id2,address2) != EOF){
    printf("Name: %s, Surname: %s, ID: %d, Address: %s\n",
        name2, surname2, id2, address2);
}

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

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