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.
%s
will stop. fgets()
, and then worry about parsing it. 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:
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. So given an input file of:
firt ast 1234 somebody@something.com
sec but 22352 goo@gle.com
thir haha 9999 nice@lly.com
And the above variable declairactions, the following code:
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("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 ,
Name,Surname , ID ,Address
Name ,Surname,ID,Address
Name,Surname,ID,Address
Then use the following string format in the 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
Name Surname ID Address
Name Surname ID Address
Name Surname ID Address
Then use the following string format in the 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.