Thread: Read text file with unknown number of rows in C

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    4

    Read text file with unknown number of rows in C

    Hi All,

    I have been having a difficult time trying to read space-delimited text file with unknown number of rows in C.

    To be a little bit specific, say I have a space delimited integer type data with known column numbers but unknown number of rows. How do I read the text file in C and store it in a 2-d array. In addition, I want to know the number of rows in the text file to be read from. This is how my text file may look like:

    data.txt (say we know it has 4 columns but number of rows unknown)
    0 1 0 1
    1 0 1 0
    1 1 0 0
    .........
    .........
    I would appreciate if some one can provide me source code or direct me to the relevant tutorials online if possible. Greatly appreciated!

    Thanks!

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Sure, you need to do something along the lines of:

    1. Open the file
    2. Check if file opened successfully
    3. while not EOF
    4. reallocate space in array
    5. read line into array
    6. repeat
    7. close file

    The file faq is here

    To dynamically size the array you need the realloc() function.

    Now for multi-dimensional arrays it is a little trickier so here is a quick sample to show you the idea:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main (void)
    {
            int **myArray;
    	    
            myArray = (int**)malloc(4 * sizeof(int*));
    	if(myArray){
    		for (int i = 0;i < 4;i++)
    			myArray[i]= (int*)malloc(sizeof(int));
    	}
    	
    	for(int i=0;i<4;i++)
    		for(int j=0;j<4;j++)
    			myArray[i][j] = 3;
    	
    	for(int i=0;i<4;i++){
    		for(int j=0;j<4;j++)
    			printf("%d", myArray[i][j]);
                              printf("\n");
                 }
    	_getch();
    	return 0;
    }
    Last edited by AndrewHunter; 06-25-2011 at 11:57 PM.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A quicker and easier approach (although not as elegant), is to just ask yourself "How many rows could there be, maximum?"

    Say, it's 200.

    So use 200 (and guess larger than you could ever expect the size to actually be), and save all the malloc/realloc work. Remember, just because you have 200 rows in your array, you don't need to have 200 lines of text. If you have just 18 rows of text, and you want to print them out, just print out 18 rows, (0 through 17), of course. As you load the data, be sure to count the rows of valid text, so there is no confusion later on.


    Yet another way is to first, count the number of rows in the file, and then malloc the exact number of rows in your array that you need. Now reset the file pointer to the start of the file, and now read in your data.

    Remember, whatever you allocate, you need to free(), and in the reverse order of the way it was allocated. That is, you allocate rows first, and then the columns for each row. When you free the memory, you free the columns first, using a loop, and then free the rows, last.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Oops, yes you do need to free that memory. Thanks Adak.
    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. How to read columns and rows
    By university in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 12:09 PM
  2. read unknown integers from text file
    By underlink in forum C Programming
    Replies: 2
    Last Post: 11-10-2009, 10:23 AM
  3. Read from file of unknown length, then terminate
    By hello234 in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 04:33 PM
  4. Is there any free text editor that displays number of rows?
    By Roy01 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-21-2006, 09:35 PM
  5. Replies: 1
    Last Post: 09-10-2005, 06:02 AM

Tags for this Thread