reading from a .txt

This is a discussion on reading from a .txt within the C Programming forums, part of the General Programming Boards category; both together ?...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    "%d %d %d %d %d %d "
    Still not updated the format to the correct one?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

Page 2 of 2 FirstFirst 12
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, 06:27 AM
  3. Need help with reading from .txt
    By Dark_Smily in forum C++ Programming
    Replies: 4
    Last Post: 01-09-2007, 11:56 PM
  4. reading strings from a binary .txt file
    By rwmarsh in forum C++ Programming
    Replies: 8
    Last Post: 03-05-2006, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21