Thread: Reading lines from a file and storing it in an array from typdef struct

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Dallas, Texas
    Posts
    8

    Reading lines from a file and storing it in an array from typdef struct

    I'm trying to read in lines for a file and storing it in an array. This is what I have so far. Am I allocating the memory correctly?

    Code:
    typedef struct {
    
    /* declartion of variables */
    } App
    
    
    typedef struct {
            App* appts[8]; /* an array of 8 pointers to typedef struct App */
            char days;  /* array that is dynamically allocated of typedef struct June */
    } June;
    
    
    int
    main()
    {
            if (argc != 3)
                    exit;
            else
            {
            FILE *fp = fopen( argv[1], "r");
    
    
            char line[150];
            int i;
            June *days;
            fgets(line, 150, fp);
      
          while(fgets(line, 100, fp) != NULL) /* reading lines from a file */
            {
                    days[i] = (char *)malloc(sizeof(char)); /* storing it in an array using malloc to allocate memory */
                    strcpy(days[i], line);
                    i++;
            }
    
    
            fclose(fp);
            }
    }
    Questions that I have:
    How do I access the array content? I'm suppose to strtok the contents from the lines stored in the array and use it again.

    Any help is appreciated. Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following snippet your comment is very wrong.
    Code:
            App* appts[8]; /* an array of 8 pointers to typedef struct App */
    That is a pointer to an array of App with a size of 8. Also where are you allocating any memory for that pointer?

    Next this snippet:
    Code:
            int i;
            June *days;
    ...
                    days[i] = (char *)malloc(sizeof(char)); /* storing it in an array using malloc to allocate memory */
    What exactly are you trying to do here?

    First days, is a instance of your structure June not a char. Second you shouldn't be casting the return value of malloc in a C program.


    Jim

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by jimblumberg View Post
    Code:
    App* appts[8]; /* an array of 8 pointers to typedef struct App */
    That is a pointer to an array of App with a size of 8. Also where are you allocating any memory for that pointer?
    It's actually fine:

    Code:
    App* appts[8]; // an array of 8 pointers to App
    App (* appts)[8]; // a pointer to an array of 8 Apps
    You should allocate like this:
    Code:
    days[i] = (June*)malloc(sizeof(*days[i])); // allocate the number of bytes necessary for June
    and always remember to free what you don't use anymore:
    Code:
    free(days[i]); // in another loop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 01-12-2013, 02:04 AM
  2. Reading a file and storing it as a 2d Array.
    By zimbomac in forum C Programming
    Replies: 16
    Last Post: 08-24-2012, 07:43 AM
  3. Reading from File and Storing into Array
    By Magic staple in forum C Programming
    Replies: 13
    Last Post: 12-05-2011, 12:00 AM
  4. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  5. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM