Weird bug in string array output

This is a discussion on Weird bug in string array output within the C Programming forums, part of the General Programming Boards category; So here's the code Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include "printFriends.h" void main(int argc, char **argv) { int ...

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

    Weird bug in string array output

    So here's the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "printFriends.h"
    
    
    void main(int argc, char **argv)
    {
      int i,x,size,wordnum,friends;
      char max[256],buffer[256];
      char *fname,*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))
        {
          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)
            {
              //Allocate array with the number of friends
              friendArray = (friend *)malloc(sizeof(friend)*atoi(prev)+1);
                 friends = atoi(prev);
                 printf("%s %s",prev,curr);
    
    
              //Insert items into array using for loop
              for(x=0;x<friends;x++)
            {
                      fgets(buffer,256,file);
                friendArray[x].firstname = strtok(buffer," ");
                friendArray[x].lastname = strtok(NULL," ");
                strcpy(friendArray[x].birthdate,strtok(NULL," "));
            }
                 defaultPrint(friendArray,friends,stdout);
            }      
        }
        } 
      fclose(file);
    }
    Here's the struct
    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);
    	
    }
    The output I get is
    0: ri, Kasturi, 03031983
    1: ri, Kasturi, 07011975
    2: , Kasturi, 08251983
    3: ri, Kasturi, 01012001
    4: Hansen, Kasturi, 10051950

    The output I want is
    Zhang, Jane 03031983
    Hernandez, John 07011975
    Smith, Wenyue 08251983
    Wassal, Jose 01012001
    Hansen, Kasturi 10051950

    My code just seems to take the last name on the list which is weird and I don't understand why.
    Last edited by leinad0213; 01-19-2012 at 01:36 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't actually copying any memory (the names) anywhere. You are just making (unknown pointers, since you didn't show us the definition of your structure) pointers point at the buffer that strtok returns. This would normally give you an error of sorts, since the function isn't creating any dynamic memory, and a normal array would have gone out of scope. Since strtok actually uses a static buffer, you get whatever is in there currently. All your name pointers end up pointing to the same space.


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

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Any idea on how I would fix that?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Switch from pointers to arrays in your structure, or dynamically allocate memory, and copy the data strtok has saved before you call it again.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird output loop+array.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 05-27-2008, 11:36 AM
  2. weird output
    By kantze in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2006, 11:05 AM
  3. my output is weird
    By panfilero in forum C Programming
    Replies: 1
    Last Post: 11-10-2005, 01:11 PM
  4. weird output using cos
    By Jan79 in forum C++ Programming
    Replies: 1
    Last Post: 06-23-2003, 07:22 AM
  5. Weird string output only for first run
    By Evilelmo in forum C++ Programming
    Replies: 4
    Last Post: 03-04-2003, 08:00 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21