Thread: Storing information into struct array

  1. #1
    Registered User
    Join Date
    Jan 2012

    Storing information into struct array

    This program is supposed to read in a text file and do the following:
    • Read in the file
    • Look for the line with the number of circles
    • Skip the number of line there are of number of circles
    • Look for line with number of friends
    • Allocate an array with just enough space for the number of friends
    • Parse and store the friends

    • I am not sure on how to parse the information into the friend array using the friend struct in defaultPrint.c
    • Please look over my code if possible to mention any bugs or syntax I may have missed

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "printFriends.h"
    void main(int argc, char **argv)
      int i,x,size,wordnum;
      char max[256];
      char *fname,*word,*friends,*line,*prev,*curr;
      FILE *file;
      friend *friendArray;
      //Check for command line arguments
      if (argc <= 1)
          printf("Insufficient number of inputs\n");
      //Open the file and check if file exists
      fname = argv[1];
      file = fopen(fname,"r");
      if (file == NULL)
          printf("Cannot open input file %s\n",fname);
      //Read in file line by line and search word
          line = fgets(max, sizeof(max), file);
          prev = strtok(max," ");
          while((curr = strtok(NULL," ")) != NULL)
    	  if (strstr(curr, "circle") != NULL)
    	      printf("%s %s",prev,curr);
    	      //skip lines amounting to number of circles
    	  if (strstr(curr, "friend") != NULL)
    	      printf("%s %s",prev,curr);
    	      //allocate array with the number of friends
    	      friendArray = malloc(sizeof(friend)*atoi(prev)+1);
                         //for loop maybe to store info into the friend
    //call defaultPrint somehow here
    #include <stdio.h>
    #include <stdlib.h>
    #include "printFriends.h"
    void defaultPrint(friend *array, int num_friends, FILE *outfile)
    	int i;
    	printf("Default print function\n");
    		fprintf(stdout,"%d: %s, %s, %s\n",i,

    #ifndef PRINTFRIENDS_H
    #define PRINTFRIENDS_H
    typedef struct _friend{
       char *lastname;
       char *firstname;
       char birthdate[9];
    } friend;
    void defaultPrint(friend *array, int num_friends, FILE *outfile);

  2. #2
    Registered User
    Join Date
    Dec 2011
    Also -> FAQ > Why it's bad to use feof() to control a loop -

    Don't use void main, always follow the standard and use int main.

    You have five unused variables in main, three ints (4 bytes each) and two char pointers (at least 4 bytes each, probably 8). By being slightly careless (and not having warnings on), you're wasting at least 20 bytes of memory.

    Nowhere are you freeing all of this memory. Even though Valgrind reports no leaks, this is probably because the flow never reaches the allocation part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-09-2008, 10:47 AM
  2. Storing Information then Displaying
    By peckitt99 in forum C++ Programming
    Replies: 11
    Last Post: 09-05-2006, 11:29 AM
  3. storing into an ARRAY OF STRUCT
    By jave in forum C Programming
    Replies: 5
    Last Post: 10-23-2005, 02:25 AM
  4. Storing information for lcd data
    By ekarapanos in forum C Programming
    Replies: 1
    Last Post: 04-02-2003, 02:04 PM
  5. Storing information in a file
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 11-18-2001, 09:05 AM

Tags for this Thread