both together ?
both together ?
yes, read each line using fgets() in a loop (see the FAQ). And parsing the line you just read using sscanf() into your struct, make sure 'i' doesn't go out-of-bounds!
hm ... i am still doin sometin wrong ...its runnin but givin me garbage outCode:while(fgets(string,24,fp) != NULL); ( sscanf(fp, "%d %d %d %d %d %d ", &(s[1].nr), &(s[1].d), &(s[1].s1), &(s[1].s2), &(s[1].s3), &(s[1].s4))); }
> while(fgets(string,24,fp) != NULL);
That ; terminates the while loop - it does nothing for the whole file.
Also, do you have
char string[24];
or did you just declare
char *string;
and pray?
> sscanf(fp, "%d %d %d %d %d %d ", &(
You're using sscanf - scan from a string.
The first parameter is a FILE* pointer, not a pointer to a string. Didn't your compiler warn you, or did you just ignore it?
The usage is
sscanf( string, "formats", params );
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ii counted 23 chars in my tablke above ... i rear that it has to be char+1 ... hence 24 ...
wpuld be very thankful if someone suggested the code !
Last edited by sdherzo; 06-25-2007 at 05:23 AM.
it should be sizeof(string),
ie
Take the time to type slower, and make your exact problem clearCode:char string[512]; while(fgets(string, sizeof(string), fp) != NULL) { /* ... */
thank u guys !
"%d %d %d %d %d %d "
Still not updated the format to the correct one?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
no i did it now :-)