-
fscanf problem
Hi I am new to C programming and I'm having some problems reading in data from a file and storing the information read in in a self-defined structure.
The data which is stored in he file is formatted as follows:
#<space>Name:<space>1\n
#<space>Name:<space>2\n
etc...
The structure I have created is very simple:
----------------------------------------
typedef struct{
int id;
char name[128];
} TYPE;
----------------------------------------
When reading in I want to ignore the hash symbol and the space at the beginning of each line, then store the name in the structure's 'name' field. Then ignore the colon character and the space which appears after the name and store the integer value in the 'id' field.
I have an array which I have defined as:
TYPE types[4] /*since there are 4 lines in the file which need reading in.*/
I then loop and read in (what I think is) each line at a time:
--------------------------------------------
int i=0;
while(i<4)
{
fscanf("%*[# ]%[a-zA-Z[-]]%*[: ]%d\n", &types[i].name, &types[i].id);
printf("%d %s\n", types[i].id, types[i].name);
i++;
}
--------------------------------------------
This compiles fine on the gcc compiler however, the problem that I get is that the output which is printed each time I go through the loop is wrong. I think it's probably my format string which is incorrect, but I have looked at numerous text books and online sources and I couldn't find any useful examples.
I would be greatfull if anybody could point me in the right direction because I don't understand why it isn't working correctly. I have tried altering my format string and I sometimes get:
0 String1
0
0
and other times I get:
1
2
3
(where no names are shown)
I hope I have described this problem clearly enough - If not please let me know.
Any help would be much appreciated. Thanks
-
1. Read the line into a buffer using fgets()
2. Parse the line using sscanf()
Code:
char buff[BUFSIZ];
while ( i < 4 && fgets( buff, sizeof buff, fp ) != NULL ) {
if ( sscanf( buff, "# %[^:]: %d", types[i].name, &types[i].id ) == 2 ) {
i++;
}
}
Remember, characters also stand for themselves in a scanf type line, so this scanf call will begin with trying to match a #
-
Hmm, perhaps "#%[^:]:%d"? When matching literal values in the format string, you don't need to use a format modifier to find and discard them. Just put them right in the string and scanf will attempt to match that character. Also keep in mind that aside from %c, %n, and %[, all of the format modifiers will skip leading whitespace.
[edit: ah, too slow]
-
Thanks very much for the speedy replies :)
Apologies for not using the code tags (as well as being new to C I am a new member to this forum)! I will have another go at the techniques you have suggested.
Thanks again! :)
-
Thank you both very much! I managed to get it working :) It turned out that my format string was infact incorrect. Changing it to
Code:
fscanf(file, "#%[^:]:%d\n", &types[i].name, &types[i].id);
worked. Thanks for the heads-up on how format strings work! This has been bugging me for several days now!!