Thread: reading from a .txt

  1. #16
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    both together ?

  2. #17
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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!

  3. #18
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    Code:
         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)));
    	
    	 }
    hm ... i am still doin sometin wrong ...its runnin but givin me garbage out

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  5. #20
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    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.

  6. #21
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    it should be sizeof(string),

    ie
    Code:
    char string[512];
    
    while(fgets(string, sizeof(string), fp) != NULL)
    {
        /* ... */
    Take the time to type slower, and make your exact problem clear

  7. #22
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    thank u guys !

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    "%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

  9. #24
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    no i did it now :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading .txt file
    By vijay85 in forum C Programming
    Replies: 5
    Last Post: 03-09-2009, 04:07 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Need help with reading from .txt
    By Dark_Smily in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2007, 12:56 AM
  4. reading strings from a binary .txt file
    By rwmarsh in forum C++ Programming
    Replies: 8
    Last Post: 03-05-2006, 09:23 PM
  5. Reading maps from a .txt file?
    By Da-Spit in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2002, 04:23 AM