I have query in C. I am sorry as this may not be the right place to post query on C.
The query is as follows..
1)
I have written following code...
main()
{
FILE *fp = 0;
char c1[100] = {0},c2[100] = {0};
int retval = 0;fp = fopen("test.s","r");
if (!fp)
{
exit(1);
}
retval = fscanf(fp,"ID%*[ \r\n\t]=%[0-9]%*[ \r\n\t]TEXT%*[ \r\n\t]=%*[ \r\n\t]{\n%[^}]%*[ \r\n\t]",c1,c2);
/* check for retval use c1 & c2 for some purpose */
fclose(fp); }
The contents of file "test.s" from which I am reading the data are ...
ID =4
TEXT =
{
hi how are you
fine
thank you
}
ID =23
TEXT =
{
abcdefg
pqrst
}
ID =3
TEXT =
{
abc
pq
}Now in above code the Regular Expression(RE) ID%*[ \r\n\t]=%[0-9] will accept ID =100 ie. it will ignore ALL white spaces from ID till =. But it will not accept ID=100 ie. NO white spaces from ID till = and the input can be ID =100 as well as ID=100.
I want to know that how to achieve this ie. it should be able to accept both ID=100 & ID =100.
So from above observation it shows that the RE %*[ \r\t\n] ignores ONE OR MORE white spaces, where as i want it to ignore ZERO OR MORE white spaces. So i have to give atlest one white space after ID till =. How to get around this.
2)
Again refering to same code now i have written fscanf inside a while loop to read all input from the file as follows...
main()
{
FILE *fp = 0;
char c1[100] = {0},c2[100] = {0};
int retval = 0;fp = fopen("test.s","r");
if (!fp)
{
exit(1);
}while (!feof(fp))
{
retval = fscanf(fp,"ID%*[ \r\n\t]=%[0-9]%*[ \r\n\t]TEXT%*[ \r\n\t]=%*[ \r\n\t]{\n%[^}]%*[ \r\n\t]",c1,c2);
if (retval == 0)
{
break;
}
/*
check for retval
use c1 & c2 for some purpose
*/
}/*while loop ends*/
fclose(fp); }
This code is expected to read each time the appropriate data from file "test.s" into c1 and c2.
But the problem is it runs successfully for first iteration of the while loop. From second iteration onwards fscanf returns 0 and doesn't fill c1 and c2 ie. it fails from second iteration onwards even if there's data inside "test.s" ie. EOF is not reached. I checked errno it shows No error. I also inserted fflush(fp) before fscanf() in while loop which makes no difference in the behaviour. This means the while loop runs only once whereas it should run till it gets the matched data (in this example it should run 3 times). I could not get around this behaviour of fscanf.
Can anyone please help me getting around these problems?
Waiting for kind reply.
-Amey Modak.
_________________________________________________________________
The MSN Survey! http://www.cross-tab.com/surveys/run/test.asp?sid=2026&respid=1 Help us help you better!
-- ______________________________________________________________________ Pune GNU/Linux Users Group Mailing List: ([email protected]) List Information: http://plug.org.in/mailing-list/listinfo/plug-mail Send 'help' to [EMAIL PROTECTED] for mailing instructions.
