fscanf

P

Petros Makris

(please forgive me i don't know how to find older messages if this is
already answered)

I am trying to understand why

fscanf( fp, "%s=%s\n", str1, str2);

doesn't work when I am trying to read from a text file the following

--------------------------------
key1=value1
key2=value2
--------------------------------

I can see that leaving a space before & after `=` it works. Shouldn't fscanf
see that?
I know i have to tokenize any line of the file with strtok() if I don't want
to use the space but i am just curious.
 
M

Michael Mair

Petros said:
(please forgive me i don't know how to find older messages if this is
already answered)

Go to google.com.
Click on "Groups"
Enter your request followed by group:comp.lang.c -- in your case
"scanset group:comp.lang.c" will be helpful -- or use the extended
search, restricting it to comp.lang.c.
I am trying to understand why

fscanf( fp, "%s=%s\n", str1, str2);

doesn't work when I am trying to read from a text file the following

--------------------------------
key1=value1
key2=value2
--------------------------------

I can see that leaving a space before & after `=` it works. Shouldn't fscanf
see that?
I know i have to tokenize any line of the file with strtok() if I don't want
to use the space but i am just curious.

Well, the requested input items take the largest possible part of the
input to match. What you request is more in the vein of regular
expressions.
If you want anything but '=', use "%[^=]" instead of "%s".
And do not use [fs]scanf() without checking the return value (number
of actually read input items).
if (fscanf(fp, "%[^=]=%s ", str1, str2) != 2) {
/* Error handling or default parameters */
}
Apart from that, it is a Good Idea to restrict the maximum number of
characters to be read so you cannot have an overflow.
Look up the manpage for [fs]scanf or the documentation in your C
book or search for "scanset" as suggested above.


-Michael
 
A

Al Bowers

Petros said:
(please forgive me i don't know how to find older messages if this is
already answered)

I am trying to understand why

fscanf( fp, "%s=%s\n", str1, str2);

doesn't work when I am trying to read from a text file the following

Right. %s Matches a sequence of non-white-space characters. It will read
right thru the '='. When you put in the space you stopped the read.
I know i have to tokenize any line of the file with strtok() if I don't want
to use the space but i am just curious.

You can try to do this using fscanf and scansets. For example for
stri use %[^=] which will stop and not read the '='
and
str2 use %s which will stop and not read the newline character.

The complete fscanf statement is listed below.

#include <stdio.h>

int main(void)
{
char s1[32], s2[32];
FILE *fp;

if((fp = fopen("test.txt","r")) != NULL)
{
while(2 == fscanf(fp," %[^=]=%s",s1,s2))
printf("s1 = %s\ns2 = %s\n\n",s1,s2);
fclose(fp);
}
else puts("Unable to open file");
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

no error by fscanf on reading from output file 18
fscanf 31
fscanf() return value 6
beginner fscanf question 10
fscanf reading lines 7
fscanf problem 2
scanf behaviour 4
fscanf problem 5

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top