Help with reading in a file

This is a discussion on Help with reading in a file within the C Programming forums, part of the General Programming Boards category; Can anyone help me with a code that reads numbers from a file and puts them into a 2-D Array? ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    11

    Help with reading in a file

    Can anyone help me with a code that reads numbers from a file and puts them into a 2-D Array?

    Basically the file has 1000 rows and 2 columns.

    For example, a section would look like:

    47 56
    20 15
    20 16
    35 76
    32 12

    So I guess can someone help me understand how to write a code that would put that data into an 2-d array? (if that's even possible) Maybe the first column of numbers into one variable and the second column into the other?

    This is the code I used when I tried to even see if I could read the file.
    Code:
    int main(void)
    {
    
        int first_number;
        int second_number;
        
        FILE* finput;
        
        finput = fopen("numbers.txt", "r");
        
        do
        {
            fscanf(finput, "%d %d ", &first_number, &second_number);
            printf("%d - %d\n", first_number, second_number);
            
        }while(!feof(finput));
        
        fclose(finput);
        
        system("PAUSE");
        return 0;
    }
    I know this isn't near what I wanted to do, but it sort of worked. Except for some reason it only read a portion of the file.

    I think I might be hopeless
    Last edited by NeedHelpWithC; 07-21-2009 at 08:45 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you think it only read a portion of the file, and which portion of the file do you think it read?

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    To read into a 2-D array, it helps to have such an array in your code

    You will need something like this:

    Code:
    int data[ NUM_ROWS ][ NUM_COLS ];
    And then the data on the M'th row, N'th column, should be stored and accessed as:

    Code:
    data[ M ][ N ];
    In your case, NUM_ROWS == 1000, and NUM_COLS == 2.

    So you'll need a loop which reads every line of the file. The line number can be indicated by a variable m, which starts at zero and increments for each line. For each line, you break it into two parts, and store these parts into slots data[ m ][ 0 ] and data[ m ][ 1 ].
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by tabstop View Post
    Why do you think it only read a portion of the file, and which portion of the file do you think it read?
    Hmm...maybe because I didn't specify what lines of data I wanted? I'm not sure how many numbers it read but it's definitely from the end of the file. I'd say maybe the last 200-300 numbers.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by NeedHelpWithC View Post
    Hmm...maybe because I didn't specify what lines of data I wanted? I'm not sure how many numbers it read but it's definitely from the end of the file. I'd say maybe the last 200-300 numbers.
    So that means it read them all, and printed them all, but your terminal window only shows the last few. Run your program and redirect it to a (different) file and you'll see all the numbers.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by brewbuck View Post
    To read into a 2-D array, it helps to have such an array in your code

    You will need something like this:

    Code:
    int data[ NUM_ROWS ][ NUM_COLS ];
    And then the data on the M'th row, N'th column, should be stored and accessed as:

    Code:
    data[ M ][ N ];
    In your case, NUM_ROWS == 1000, and NUM_COLS == 2.

    So you'll need a loop which reads every line of the file. The line number can be indicated by a variable m, which starts at zero and increments for each line. For each line, you break it into two parts, and store these parts into slots data[ m ][ 0 ] and data[ m ][ 1 ].
    Maybe something close to this?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int m;
        int n;
        int array[1000][2];
        
        FILE* finput;
        
        finput = fopen("numbers.txt", "r");
        
        for(m = 0; m < 1000; m++)
        {
            for(n = 0; n < m; n++)
            {
                fscanf(finput, "%d ", &array[m][n]);
                printf("%d\n", array);
            }
        }
        
        fclose(finput);
        
        system("PAUSE");
        return 0;
    }

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Almost correct, except that this:

    Quote Originally Posted by NeedHelpWithC View Post
    Code:
            for(n = 0; n < m; n++)
    Should be this:

    Code:
            for(n = 0; n < 2; n++)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by tabstop View Post
    So that means it read them all, and printed them all, but your terminal window only shows the last few. Run your program and redirect it to a (different) file and you'll see all the numbers.
    Cool, the file I wrote to did have all the numbers So what exactly does this mean? I can just trust that my program reads all the numbers but I cant expect it to print all 1000 numbers?

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by brewbuck View Post
    Almost correct, except that this:



    Should be this:

    Code:
            for(n = 0; n < 2; n++)
    Oh okay, I see. It's still giving me weird numbers. Did I write the array part right? ("&array[m][n]")

    I feel like that's wrong.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by NeedHelpWithC View Post
    Cool, the file I wrote to did have all the numbers So what exactly does this mean? I can just trust that my program reads all the numbers but I cant expect it to print all 1000 numbers?
    That's right, a command prompt/shell/whatever won't have 1000 lines available to it. (There might be a way to extend the amount you have, but either I've forgotten it or never knew it.)

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by NeedHelpWithC View Post
    Oh okay, I see. It's still giving me weird numbers. Did I write the array part right? ("&array[m][n]")

    I feel like that's wrong.
    That's fine -- your compiler should be telling you that printf("%d", array) is wrong -- since array is not an int.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by tabstop View Post
    That's fine -- your compiler should be telling you that printf("%d", array) is wrong -- since array is not an int.
    it didn't lol.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by NeedHelpWithC View Post
    it didn't lol.
    Did you perhaps forget to #include <stdio.h>? Most (modern) compilers should warn when a printf specifier doesn't match its argument.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by brewbuck View Post
    Did you perhaps forget to #include <stdio.h>? Most (modern) compilers should warn when a printf specifier doesn't match its argument.
    Nope, that wasn't it. I used two nested for loops. One for scanning in the number and the next one to print them.

    It looks like this now.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int m;
        int n;
        int array[1000][2];
        
        FILE* finput;
        
        finput = fopen("numbers.txt", "r");
        
        for(m = 1; m < 1000; m++)
        {
            for(n = 0; n < 2; n++)
            {
                fscanf(finput, "%d ", &array[m][n]);
            }
        }
        
        for(m = 1; m < 1000; m++)
        {
            for(n = 0; n < 2; n++)
            {
                printf("%d", array[m][n]);
            }
            printf("\n");
        }
        
        fclose(finput);
        
        system("PAUSE");
        return 0;
    }

  15. #15
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Thank you for all the help tabstop and brewbuck!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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