Thread: Storing information into struct array

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    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

    Issues:
    • 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

    Code:
    #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");
          exit(1);
        }
    
    
      //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);
          exit(2);
        }
    
    
      //Read in file line by line and search word
      while(!feof(file))
        {
          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
    	      for(i=1;i<atoi(prev);i++)
    		printf("\n");
    	    }
    	  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(i=0;i<atoi(prev);i++)
    		{
                         //for loop maybe to store info into the friend
                         //struct/array???
    		}
    	    }
    	}
        }
    //call defaultPrint somehow here
      fclose(file);
    }
    defaultPrint.c
    Code:
    #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");
    	for(i=0;i<num_friends;i++)
    		fprintf(stdout,"%d: %s, %s, %s\n",i,
    			array[i].lastname,
    			array[i].firstname,
    			array[i].birthdate);
    	
    }
    printFriends.h

    Code:
    
    
    #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);
    #endif

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

    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