reading from a text file into a 2 Dimensional Array---JUNK VALUES!!??

This is a discussion on reading from a text file into a 2 Dimensional Array---JUNK VALUES!!?? within the C Programming forums, part of the General Programming Boards category; Originally Posted by kal123456 @Adak so i fixed up my code a little, and now it prints the table just ...

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    985
    Quote Originally Posted by kal123456 View Post
    @Adak so i fixed up my code a little, and now it prints the table just like it is in the .txt file, but I STILL get the stupid junk values at the end!!! WHY?!
    In your approach you are walking through the file once to determine the number of rows, then you try to read through it further even though you're already at the end. Also, the code you have gives too many rows. If sales.txt has 16 rows of data, your code tells me that it has 17.

    Here's how I would do it. This determines the number of rows and then places your file pointer on the first row of data, ready for you to read in:
    Code:
    #define FILENAME "sales.txt"
    #define EXIT_FILE_ERROR (1)
    #define EXIT_UNEXPECTED_EOF (2)
    #define EXIT_INVALID_FIRSTLINE (3)
    #define MAXLINE (10000)
    char line[MAXLINE];
    
    int main(void)
    {
        // Open file
        FILE *fp;
        if ((fp = fopen(FILENAME, "r")) == NULL)
            exit(EXIT_FILE_ERROR);
        
        // Figure out how many columns there are
        int cols;
        if ((fgets(line, sizeof(line), fp)) == NULL)
            exit(EXIT_UNEXPECTED_EOF);
        if ((sscanf(line, "%d", &cols)) != 1)
            exit(EXIT_INVALID_FIRSTLINE);
        
        // Read each line in the file to figure out how many rows there are
        int rows = 0;
        while ((fgets(line, sizeof(line), fp)) != NULL) {
            if (strlen(line) == 1) // don't count blank lines
                continue;
            rows++;
        }
        
        // Rewind to beginning and then skip the first line
        rewind(fp);
        fgets(line, sizeof(line), fp);
        
        // Allocate correctly-sized array
        char array[rows][cols];
        printf("number of rows is %d and number of columns is %d\n", rows, cols);
        
        // TODO
        
        fclose(fp);
        return EXIT_SUCCESS;
    }

  2. #17
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    @c99tutorial Hey, your code looks wonderful! It's so organized! I'm actually trying to calculate the number of rows and columns just for myself, my teacher doesn't require it, but I think it would be good practice. I tried seeing if your code worked, and it gives me several errors when I try to run it:

    \\puma\home\kal123456\blah.c||In function 'main':|
    \\puma\home\kal123456\blah.c|11|error: 'FILE' undeclared (first use in this function)|
    \\puma\home\kal123456\blah.c|11|error: (Each undeclared identifier is reported only once|
    \\puma\home\kal123456\blah.c|11|error: for each function it appears in.)|
    \\puma\home\kal123456\blah.c|11|error: 'fp' undeclared (first use in this function)|
    \\puma\home\kal123456\blah.c|12|error: 'NULL' undeclared (first use in this function)|
    \\puma\home\kal123456\blah.c|13|warning: incompatible implicit declaration of built-in function 'exit'|
    \\puma\home\kal123456\blah.c|19|warning: incompatible implicit declaration of built-in function 'sscanf'|
    \\puma\home\kal123456\blah.c|25|warning: incompatible implicit declaration of built-in function 'strlen'|
    \\puma\home\kal123456\blah.c|36|warning: incompatible implicit declaration of built-in function 'printf'|
    \\puma\home\kal123456\blah.c|41|error: 'EXIT_SUCCESS' undeclared (first use in this function)|
    ||=== Build finished: 6 errors, 4 warnings ===|


    I'm wondering what could i change to fix it?.. I don't see any mistakes at all haha....

  3. #18
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,304
    Sounds like you forgot to include any header files...
    Fact - Beethoven wrote his first symphony in C

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

Similar Threads

  1. Reading values from text file
    By a.mlw.walker in forum C Programming
    Replies: 5
    Last Post: 01-14-2012, 05:02 PM
  2. Replies: 3
    Last Post: 10-21-2010, 12:39 PM
  3. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 06:15 PM
  4. Replies: 6
    Last Post: 11-11-2006, 01:10 PM
  5. Reading a file into a two dimensional array!!
    By Dilmerv in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2006, 01:32 PM

Tags for this Thread


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