Thread: Reading a txt file into an array: unexpected segfault

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    7

    Reading a txt file into an array: unexpected segfault

    Hi to all!

    I have this function that, provided with a target array of strings and a file name, reads it line by line and stores the lines into the given array

    Code:
    void file_to_array(char **target_lines, char *file_name)
    {
    	FILE *f;
    	char line[LINE_SIZE];
    	int num_righe = 0;
    	
    	f = fopen(file_name, "r");
    	/* crea il file se non lo trova */
    	if(f == NULL) {
    		f = fopen(file_name, "w");
    	}
    	
    	while(fgets(line, LINE_SIZE, f)) {		
    		num_righe++;
    		target_lines = (char**)realloc(target_lines, (sizeof(char*)*num_righe));
    		target_lines[num_righe-1] = strdup(line);
    	}
    
    	fclose(f);
    }
    Calling it like follows it gives me a Segmentation fault in the line where I try to print the second item.

    Code:
    char **lines = NULL;
    file_to_array(lines, "spese.dat");
    printf("%s", lines[0]);
    The file is correct and outputting like by line within the function ( printf("%s", target_lines[num_righe-1]) ) gives the correct lines.

    Any help?
    Thanks in advance
    Last edited by pistacchio; 05-05-2009 at 01:14 PM. Reason: colored the faulting line

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Changes made by the called function to lines do NOT make it back to the caller.

    Try something like
    lines = file_to_array ( lines, "filename" );

    And put
    return lines;
    at the end of the function.

    > f = fopen(file_name, "w");
    Erm, you're reading from the file, so this won't work anyway.

    Oh, and there's no need to cast the result of malloc functions in a C program, if you've done the right thing of including stdlib.h.
    See the FAQ
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    7
    Oh, this worked, thanks! And of course the function declaration now reads like:

    char** file_to_array(char **target_lines, char *file_name)

    Out of curiosity, as I'm learning C right now: how can I do the same without a return value but passing a reference to the array (that is what I was trying to do)?
    Thanks again

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by pistacchio View Post
    Oh, this worked, thanks! And of course the function declaration now reads like:

    char** file_to_array(char **target_lines, char *file_name)

    Out of curiosity, as I'm learning C right now: how can I do the same without a return value but passing a reference to the array (that is what I was trying to do)?
    Thanks again
    Add a level of indirection:
    Code:
    void file_to_array(char ***target_lines, const char *filename)
    ...
    file_to_array(&lines, "my file");
    You'll have to deal with the extra level of indirection in the function, of course. (Get pointers down - once you do, this sort of thing should be obvious.) In practice, it'll be easier to do what Salem suggested and return a pointer. (It'll make your code in the function much clearer as well.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from a file into an array
    By fmsguy06 in forum C Programming
    Replies: 6
    Last Post: 10-18-2008, 09:25 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. reading from file to an array - help needed!
    By timeforheroes in forum C Programming
    Replies: 2
    Last Post: 04-28-2005, 12:16 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

Tags for this Thread