Thread: Reading an unknown number of data points from a table

  1. #1
    Registered User
    Join Date
    Sep 2011

    Reading an unknown number of data points from a table

    Hi there, I'm trying to read an unknown number of data points from txt based file into a 2d array. The file contains a grid of numbers which has 4 columns and an unknown set of rows. To start with I read just the first 20 rows and it worked. I am now unsure of what the code would be to read the rows until there is no more data.

    #include <stdio.h>
    #include <stdlib.h>
    int main()
        FILE *myfile;
        int j,i;
        float c[20][4];
        myfile = fopen("data.txt","r");
                puts("data.TXT not found!");
        j = 0;
        while (j<20)
                while (i<4)
                j = j+1;
        return 0;
    Any help would be much appreciated

  2. #2
    Join Date
    Aug 2010
    Ontario Canada
    If you are reading a set of fixed columns you can use fscanf() as...

    fscanf(myfile,"%f %f %f %f", &c[j][0],&c[j][1],&c[j][2],&c[j][3]);
    Also note that you should be checking the return value of fscanf() to make sure you're getting the right number of conversions. (Details will be in your library documentation.)

    BUT... when you have an unknown number of rows you have a much larger problem. You are going to have to set up a linked list with one row in each structure and read the file row by row (as above), creating a new list element for each.

    typedef t_rows
      { float c[4];
         t_rows *next
      } rows, *prows;
    You can study up on linked lists.... here

    If you absolutely need this to end up in an array... you can count the number of rows from your linked list and use malloc() to make the appropriate array and copy the data out as you destroy your linked list.

  3. #3
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    Well, you could either declare an array that is way larger than you will ever need, or if memory is a concern take a look at determining the file size at run time and then dynamically allocate your array. Look at the ftell() function. Note this is an approximation for what you would need in txt files however it will do the trick.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file with unknown number of rows in C
    By Raymond2010 in forum C Programming
    Replies: 3
    Last Post: 06-25-2011, 11:27 PM
  2. Dynamic array of unknown number of dimensions
    By gamask in forum C Programming
    Replies: 5
    Last Post: 04-29-2011, 08:42 PM
  3. help! calculate arbitrary number of floating points
    By cakestler in forum C Programming
    Replies: 5
    Last Post: 02-26-2009, 02:47 PM
  4. Reading in an unknown number of numbers
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2006, 04:21 AM
  5. unknown number of chars
    By Garfield in forum C Programming
    Replies: 16
    Last Post: 10-31-2001, 05:46 AM