Thread: reading file into a string array

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    reading file into a string array

    I'm trying to read in a bunch of strings into an array from disk and then having the program write them back to a file. My problem is that it fails while trying to read and I don't know what's causing it.

    Code:
    #define MAX 5
    
    void loadfile(char * array[MAX])
    { /* Reads in file */
    	int count;
    	FILE * fp;
    
    	fp = fopen("strings.txt","r");
    	for(count = 0; count < MAX;count++)
    	{
    	fscanf(fp,"%s ",array[count]);
    	}
    	fclose(fp);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have an array of character pointers, but do they actually point at anything?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Read the whole file to memory and then divide up into strings by finding the \r or \n.

    Check the string array you are passing into the function as I do not think you can do that the way you are.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    I'm trying to initialize the array by reading the file into it. Isn't this possible with pointers?

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    thanks, that worked.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    while( i < MAX && fgets( buf, 1024, fp ) != NULL )  { 
        array[i] = malloc( strlen(buf) + 1 ); /*maybe you haven't allocated the strings*/ 
        strcpy( array[i++], buf ); 
    }
    Points to note
    1. feof returns true only when some read function (in this case fgets) has previously failed.
    2. you need to malloc the length of the string + 1 to reserve space for the \0

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    I must free that memory like any other allocation, right?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I must free that memory like any other allocation, right?
    yes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Reading a mixed Text and decimal file into an array
    By djamie in forum C Programming
    Replies: 3
    Last Post: 08-05-2003, 06:25 AM