Reading File

This is a discussion on Reading File within the C Programming forums, part of the General Programming Boards category; I am trying to read a file and store each line in an array, but I am having a bit ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Reading File

    I am trying to read a file and store each line in an array, but I am having a bit of trouble doing it.

    This is my attempt, I know there must be a much simpler way to do this but after a few hours searching google, I could't figure out how to use malloc on 2D arrays.

    When I run the code I only get the last line of the text file printing out, not all if like I am trying to do.

    Code:
    typedef struct{
        char text[50];
    } line;
    
    line *textfile;
    FILE *f;
    
    int main(int argc, char *argv[]){
        f = fopen(argv[1],"r");
        textfile = malloc(sizeof(line));
        fgets(textfile[0].text,50,f);
        int numlines = 1;
        while(!feof(f)){
            textfile = malloc(sizeof(line)*(numlines+1));
            fgets(textfile[numlines].text,50,f);
            numlines++;
        }
        int n = 0;
        while(n<numlines){
            printf("%s",textfile[n].text);
            n++;
        }
    
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    > textfile = malloc(sizeof(line)*(numlines+1));
    You keep losing all the lines you´ve copied so far.
    Look into using realloc instead.

    > while(!feof(f))
    There is a FAQ on why this is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Okay, so my code now looks like

    Code:
    typedef struct{
    	char text[50];
    } line;
    
    
    int main(int argc, char *argv[]){
    	f = fopen(argv[1],"r");
    	textfile = malloc(sizeof(line));
    	fgets(textfile[0].text,50,f);
    	int numlines = 1;
    	textfile = realloc(textfile,2*sizeof(line));
    	while(fgets(textfile[numlines].text,50,f) != NULL){
    		textfile = realloc(textfile,2*sizeof(line));
    		numlines++;
    	}
    The way I understand realloc is that the second argument was how much additional space is to be added, but when try to add sizeof(line) it just crashed so I changed it to 2*sizeof(line)

    But when I compile and run the code, it doesn't store the third line in the text file. Every other line is done fine.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    realloc() doesn't ask for how much space is to be added. It wants the total amount of space to be allocated. So you will have to keep a running "size of buffer" count and add to that before calling realloc().

    Code:
    int BuffSize = 0;
    
    // in your loop
    BuffSize += (strlen(NewLine) +1);
    realloc (Buffer, Buffsize);
    The LineSize +1 allows for the trailing null on C strings.

    I would also suggest that you read your text into a single fixed buffer with some extra space and then use strcpy() to move it to your array. If your expected length is 50 chars, allocate at least 100 to this input buffer. This is to guard against lines that are longer than expected or lines that are missing EOL markers.
    Last edited by CommonTater; 10-21-2010 at 10:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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