Thread: Problem in reading data from file

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    Problem in reading data from file

    Hi, I need help with this C code. basically I'm trying to read a data file that I dont know the number of data, so I am trying to count the number of data and row and trying to create a multidimensional array to store the data. The problem is when I create the array with the number of column and array that the program counted, the number messes up! Please help give me some ideas of why that happen.. any help is appreciated! thank you!

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include "quicksort.h"
    
    int main(int argc, char *argv[]){
        FILE *infile, *outfile;
        int i, j, window_width;
        float **orig_data;
        
       //Error Checking
        if(argc != 3)
        {
            fprintf(stderr, "ERROR: Not enough arguments!\n");
            return 1;
        }
    
        int num = atoi(argv[2]);
        if (num == 3 || num == 7 || num == 11)
        {
            window_width = num;
        }
        else
        {
            fprintf(stderr,"ERROR: The second argument is not an option!\n");
            return 1;
        }
        float temp;
        int count;
    
        //File reading
        infile = fopen(argv[1], "r");
        if (infile == NULL)
        {
            fprintf(stderr, "ERROR: The file does not exist!\n");
            exit(EXIT_FAILURE);
        }
    
        /* Count all data in file */
        while(fscanf(infile, "%f", &temp) != EOF)
            {
             printf("count");
             count++;
             printf(" %f\n", temp);
            }
        printf("data %i\n", count);
    
        char tmp;
        int row;
       rewind(infile);
        /* count number of row */
    
        while (fscanf(infile, "%c", &tmp) != EOF)
        {
            if(tmp == '\n')
              {
               row++;
              }
        }
        printf("row %i\n", row);
    
        /* find number of column */
        int column;
        column  = count/row;
        printf("column %i\n", column);
    
        rewind(infile);
        while(fscanf(infile, "%f", &temp) != EOF)
            {
             printf("%f\n", temp);
            }
        orig_data = (float **)calloc(row, sizeof(float *));
        for (i=0; i<row; i++)
        {
            orig_data[i]=(float *)calloc(column, sizeof(float));
        }
    
        rewind(infile);
        float tmp2;
        while(fscanf(infile, "%f", &tmp2) != EOF)
        {
        for(i = 0; i<row; i++)
            {
             for(j=0; j<column; j++)
              {
               orig_data[i][j] = tmp2;
              }
            }
    
         }
    
        for(i = 0; i<row; i++)
        {
           for(j=0; j<column; j++)
           {
             printf("%f", orig_data[i][j]);
            }
          printf("\n");
        }
    
        fclose(infile);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Please don't post duplicate threads...

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    Please don't post duplicate threads...
    wait, I'm sorry what?
    I am new here. how did i duplicate the thread?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Narrow down the problem and save us some time, OK?

    What part is NOT OK?

    1) reading in the data
    2) creating the dynamic array
    3) loading the data, into the dynamic array, correctly. (You can print it out OK)
    4) sorting or doing other work with the data, after it's been loaded correctly.

    The more specific you can be, the quicker we can narrow down what the problem is, and save a LOT of time. You need to practice this kind of troubleshooting technique yourself, btw. Just reading a book or taking a class, is not the same as actual troubleshooting practice on code.

    We have new forum software, and I don't see any duplicate threads you've started - so I'd not worry about it.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anilzaf View Post
    wait, I'm sorry what?
    I am new here. how did i duplicate the thread?
    Apologies... apparently there's bit of a tussle with some forum features right now, due to an upgrade.
    I was unaware of that when I wrote my comment.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    What part didn't work? You are displaying the row, colum. Are they correct?

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Sorry for not being too specific....

    Basically the code works fine if I remove this part from the code.

    Code:
        orig_data = (float **)calloc(row, sizeof(float *));
        for (i=0; i<row; i++)
        {
            orig_data[i]=(float *)calloc(column, sizeof(float));
        }
    
        rewind(infile);
        float tmp2;
        while(fscanf(infile, "%f", &tmp2) != EOF)
        {
        for(i = 0; i<row; i++)
            {
             for(j=0; j<column; j++)
              {
               orig_data[i][j] = tmp2;
              }
            }
    
         }
    
        for(i = 0; i<row; i++)
        {
           for(j=0; j<column; j++)
           {
             printf("%f", orig_data[i][j]);
            }
          printf("\n");
        }
    
        fclose(infile);
    I want to create the multidimensional array based on the row and column that the program counted. But when I added that part of code above, the row or data gave huge number. So, I am not sure what went wrong when creating the array after counting the row and column of the data. The sample textfile that I used to test is simply like this:

    1 2 3
    4 5 6
    7 8 9

    so basically the the number of data is 9 and row is 3. Then the column is 9/3 = 3. Then I want to create a 2D array of size[3][3]. Please let me know if still not clear enough. Thank you

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    Apologies... apparently there's bit of a tussle with some forum features right now, due to an upgrade.
    I was unaware of that when I wrote my comment.
    That's fine. I think there was an error the first time I posted the question. I tried re-post and probably there are two same posts, maybe? I dont see the other one though.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Ok. So many little things wrong...
    - initialize count = 0
    - initialize row = 0
    - you need a rewind(infile); before the final reading loop
    - get rid of while(fscanf(infile, "%f", &tmp2) != EOF) just before the for (i = ...
    - make final loop so:
    Code:
    for(i = 0; i<row; i++)
        {
        for(j=0; j<column; j++)
            {
            fscanf(infile, "%f", &tmp2);
            orig_data[i][j] = tmp2;
            }
        }
    - add a space in the printout: printf("%f ", orig_data[i][j]); <---- just before the closing quote like here

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Quote Originally Posted by nonoob View Post
    Ok. So many little things wrong...
    - initialize count = 0
    - initialize row = 0
    - you need a rewind(infile); before the final reading loop
    - get rid of while(fscanf(infile, "%f", &tmp2) != EOF) just before the for (i = ...
    - make final loop so:
    Code:
    for(i = 0; i<row; i++)
        {
        for(j=0; j<column; j++)
            {
            fscanf(infile, "%f", &tmp2);
            orig_data[i][j] = tmp2;
            }
        }
    - add a space in the printout: printf("%f ", orig_data[i][j]); <---- just before the closing quote like here
    It works perfectly now! Thank you very much nonoob! I appreciate your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  2. Reading from a file and using the data
    By Ste_Mulv in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 07:44 AM
  3. reading data from a txt file
    By 182blink in forum C Programming
    Replies: 9
    Last Post: 07-12-2006, 10:11 PM
  4. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  5. Replies: 20
    Last Post: 06-12-2005, 11:53 PM

Tags for this Thread