Thread: Trying to fill and print array of strings (array of pointers)

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Trying to fill and print array of strings (array of pointers)

    What I'm trying to do is read data in from a file into an array of strings then print out that array. I had to dynamically allocate the memory without knowing how many strings are in the file, so i'm trying to find the number of strings first to pass to GetMemory ().

    This is my code so far an it doesn't work. I don't really know what I'm doing here.

    Code:
    #include <stdlib.h> 
    #include <stdio.h> 
     
    int main (int argc, char** argv) 
    { 
       int s, t; 
       int numStrings = 0; 
       int i = 0; 
       FILE *ifp; 
       char string[50]; 
       char temp[50]; 
     
       /* Check correct number of command line arguments */ 
       if (argc != 2) 
       { 
          fprintf (stderr, "This program requires a command line argument\n"); 
          fprintf (stderr, "that is the name of the input file to be\n"); 
          fprintf (stderr, "used.\n"); 
          exit (-1); 
       } 
     /* Open the input file */ 
       ifp = fopen (argv[1], "r"); 
       if (ifp == NULL) 
       { 
          fprintf (stderr, "Couldn't open input file:\n"); 
          fprintf (stderr, "%s\n", argv[1]); 
          exit (-2); 
       }    
        
       while ( (fgets (temp, 50, ifp) ) != EOF) 
       { 
         numStrings++;  
          t = fgets (temp, 50, ifp); 
       }    
     
       GetMemory (numStrings, &string); 
     
       /* copy characters from input file */ 
       while ( (fgets (string, 50, ifp) ) != EOF) 
       { 
          string[i] = s; 
          i++; 
         s = fgets (string, 50, ifp); 
          fprintf (stderr, "%s", string[i]); 
       }  
     
       /* Close the file */ 
       fclose (ifp); 
     
       return 0; 
    }  
    
    /********************************************** 
    * Function: GetMemory 
    * 
    * This function gets the memory space needed and 
    * modifies the pointer that will point to it in 
    * the calling function using call by reference 
    * 
    * Inputs: Number of characters for which to allocate 
    *         space, and the pointer to the pointer that 
    *         will point to the memory 
    * Outputs: None 
    **********************************************/ 
    void GetMemory (int count, char **stringPtr) 
    { 
       /* malloc the memory and set the caller's pointer to point to it */ 
       *stringPtr = (char *) malloc( count * sizeof(char) ); 
       if (*stringPtr == NULL) 
       { 
          fprintf(stderr, "Out of memory.\n"); 
          exit (-1); 
       } 
    }
    Please help me fix this. Thank you for your time.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You should change char string[50] to char *string[50]. Also you should change the name string to something more portable since in C++ string is a type.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually there are some other issues I see. For one, you can loop through a file multiple times, such as you are doing, however, you need to call fseek(ifp, 0, SEEK_SET). What you are doing in your code is allocating on a per-line basis, right? The problem is that you are allocating the same amount of space for each line... well that isn't necessarily a problem, but the problem is that you are allocating the amount of space per line as there are lines in the file. So I don't think that is what you are meaning to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  3. Writing a C program for class....
    By Bizmark in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 10:31 AM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. C Filling and Printing a 2-d array
    By Smoot in forum C Programming
    Replies: 3
    Last Post: 11-13-2003, 08:42 PM