input data from file into matrix?

This is a discussion on input data from file into matrix? within the C Programming forums, part of the General Programming Boards category; hi......I'm terribly new to programming and have been trying to figure out the proper command to no avail. My input ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    input data from file into matrix?

    hi......I'm terribly new to programming and have been trying to figure out the proper command to no avail. My input data is something like the following:
    apple bee c .....
    1.2 2.3 0 .....
    2.4 3.5 3.2 ....
    .... ..... .....
    So the first line are strings that are 'labels' for the columns of numerical data which I want to store in an array. The numerical data I want to store in an matrix. One of my problems is that before opening the file I don't know how many rows or columns the data will have.
    This is all I've come up with at the moment. I think I need to use fscanf somehow......but have been very confused by how to use that. Thanks in advance for any help.

    Code:
      FILE *file; 
      file = fopen("data/hello.txt", "r");
      if(file==NULL) {
        printf("Error: can't open file.\n");
        return 1;
      }
      else {
        printf("File opened. Now read file\n"); 
    
          //read first line as string and process to count number of columns, then read all the lines
            following that to get number of rows of the data until eof
          //use malloc to create matrix of double of size row x column (I have a function for this elsewhere)
          //start at the beginning of the file and loop through to read each line and somehow store into array[row][col]
      fclose(file);
    I apologize in advance for my poor attempt at writing the code for this part. If anybody could help at all with how to read in a line and store the value into an array that would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There will be one column of numbers for each string?

    This is not code, it's just ideas for code!
    Code:
    char buff[BUFFSIZE];
    int col_num = 0, r, c;
    int **A;
    
    fgets(buff, "%s", BUFFSIZE - 1; fileptr);
    
    while(ok = (sscanf(buff, "%s", &garbage)) > 0) {
      col_num++;
    }
    Now col_num should have your number of columns. Usually, it's no problem figuring out the column number, because the data itself will be ordered by rows, so when you are reading in the data, and you hit a newline char, then you know that's how many columns there are for that row.

    Longest row, wins! (tells you how many columns there has to be in your matrix).

    More ramblings:
    Code:
    You can use fgets to count your rows up, but save your column headers separately, so you'll have them, for later.
    
    Once you know the # of rows and columns:
    
    *A = malloc your outer row of pointers for A sizeof(int *) 
    
    A = malloc your inner column of integers for A using sizeof(int) this time
    
    and read it into your array:
    
    for(r = 0; r < rows; r++)
       for(c = 0; c < cols; c++) 
           fscanf(fileptr, "%d", &A[r][c]);
    Now you can get the column headers still in buff, (again with sscanf(), and print them, then print up a line of =========, and then start with your rows and cols of data, in a very similar way to how they were fscanf()'d from the file, above.



    All the above is NOT code, it's just thinking aloud some thoughts in pseudo codish stuff, to give you some idea's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Matrix Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 05:21 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM

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