Like Tree1Likes

How to read from a file line by line and store lines in separate arrays?

This is a discussion on How to read from a file line by line and store lines in separate arrays? within the C Programming forums, part of the General Programming Boards category; say the file contains: 890809 8989 1343434324 4738294 im trying to read line by line and store each into separate ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    29

    How to read from a file line by line and store lines in separate arrays?

    say the file contains:
    890809
    8989
    1343434324
    4738294

    im trying to read line by line and store each into separate arrays. not working...

    Code:
    char str_out[10][40]
    file_01=fopen("test_01.txt","r");
    
    for(i=0; i<8; i++){
    fscanf(file_01, "%s\n", str_out[i]);
    printf("%s",str_out[i]);}

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by vegan View Post
    im trying to read line by line and store each into separate arrays. not working...
    Your code as it is doesn't even know if the file was opened correctly nor not...
    Test the return value of fopen() to be sure.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    1. Semicolon missing after char[10][40]

    2. \n missing after the %s in your printf statement

    3. How are you testing for the end of the file?
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    thats not a problem, suppose it opened correctly. for loop is a problem...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,197
    (1) "Suppose it opened correctly" is a dangerous assumption to make - I would listen to CommonTater here.
    (2) TheBigH makes some good observations that should be corrected, if this is your actual code (and not a hasty re-write for purposes of the post).
    (3) "not working" gives us no insight the possible issue at hand - could you clarify exactly how it is not working?

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    semicolon is there in my original code, compiler compiles everything fine. it just doesn't print the string. for loop isnt working, it should work with or without \n in printf which is just to test if it stored anything in the string. so the way it looks, i mean logically it should work, right? im just not too sure if i constructed the for loop correctly. ok here is what i have in my actual code.
    Code:
    char str_out[10][40];
    
    if((file_01 = fopen("test_01.txt", "r")) == NULL) {
            printf("Can't open file");
            exit(1);
        }
    while ( (c=getc(file_01))!=EOF )
    for(i=0; i<8; i++){
    fscanf(file_01, "%s\n", str_out[i]);
    printf("%s\n",str_out[i]);}
    printf("plz be gentle with me, im still learning");
    Last edited by vegan; 08-07-2011 at 06:44 PM.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    It works OK when I compile it (after inserting the missing declarations for i, c, and file_01). The only problem I get is when the program runs off the end of the file if there are less than 8 lines to it. It then prints out gibberish, which is what I would expect. You should put the fscanf statement inside the while conditional to check for EOF.

    edit: actually, scratch that. Get rid of the for loop altogether and replace it with a while loop that tests for EOF.
    Last edited by TheBigH; 08-07-2011 at 06:45 PM.
    Code:
    while(!asleep) {
       sheep++;
    }

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    Thanks a lot TheBigH. if you made it work, then i should be able to make it work too. thanks everyone! PS. found the problem
    Last edited by vegan; 08-07-2011 at 06:56 PM.
    TheBigH likes this.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by vegan View Post
    semicolon is there in my original code, compiler compiles everything fine. it just doesn't print the string. for loop isnt working, it should work with or without \n in printf which is just to test if it stored anything in the string. so the way it looks, i mean logically it should work, right? im just not too sure if i constructed the for loop correctly. ok here is what i have in my actual code.
    Code:
    char str_out[10][40];
    
    if((file_01 = fopen("test_01.txt", "r")) == NULL) {
            printf("Can't open file");
            exit(1);
        }
    while ( (c=getc(file_01))!=EOF )
    for(i=0; i<8; i++){
    fscanf(file_01, "%s\n", str_out[i]);
    printf("%s\n",str_out[i]);}
    printf("plz be gentle with me, im still learning");
    First your while loop is using getc() as your end of file test. This is completely doomed to failure. getc() reads one character from the file for each call... this in turn displaces your file pointer by one character per call and leaves fscanf() reading from the wrong position.

    Next you are reading 8 lines from the file for every single EOF test... also doomed to failure.

    Look up fscanf() and fgets() in your C library documentation... you'll find a better way.

  10. #10
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by vegan View Post
    Thanks a lot TheBigH. if you made it work, then i should be able to make it work too. thanks everyone! PS. found the problem
    Good, well done.
    Code:
    while(!asleep) {
       sheep++;
    }

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TheBigH View Post
    It works OK when I compile it
    Lesson #1 in C programming.... "Compiles" does not mean "Works".

  12. #12
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by CommonTater View Post
    Lesson #1 in C programming.... "Compiles" does not mean "Works".
    Did you properly read my post, and the one I was replying to? I know the code wasn't right because it still ran off the end of the file, which I mentioned. The complaint was that it wasn't printing the strings in the input file, but when I compiled and ran it it did print them.
    Code:
    while(!asleep) {
       sheep++;
    }

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    Thank you guys once again, I've searched half the internet looking for fscanf and fgets examples and couldn;t really find one that would help me. i just realized i shouldve prob searched for something like fscanf delimiter not just fscanf c programming.

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    Quote Originally Posted by CommonTater View Post
    First your while loop is using getc() as your end of file test. This is completely doomed to failure. getc() reads one character from the file for each call... this in turn displaces your file pointer by one character per call and leaves fscanf() reading from the wrong position.
    yep, you are right. if i had time i would find a better way to do it and i will def look into it but i need to submit my assignment soon and i also have a bunch of other assignments and stupid essays due on other subjects which is pity cos they make us study unnecessary cr*p instead of focusing more on programming.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    2,197
    Quote Originally Posted by vegan View Post
    Thank you guys once again, I've searched half the internet looking for fscanf and fgets examples and couldn;t really find one that would help me. i just realized i shouldve prob searched for something like fscanf delimiter not just fscanf c programming.
    These are examples, really, but you should bookmark and review these pages (the second is worth an in-depth study when you have the time).

    The 'C' Library Referencing Guide
    Frequently Asked Questions

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read a line and store it into an array?
    By david.jones in forum C Programming
    Replies: 5
    Last Post: 05-09-2011, 05:07 PM
  2. Read file line by line and interpret them
    By sombrancelha in forum C Programming
    Replies: 8
    Last Post: 03-17-2011, 09:48 AM
  3. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 09:51 AM
  4. Read line by line from file with EOL different that OS EOL
    By hanniball in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2010, 09:06 AM
  5. Read first line of a file and store in a variable
    By Saeid87 in forum C Programming
    Replies: 5
    Last Post: 06-19-2009, 11:27 AM

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