help with strtok

This is a discussion on help with strtok within the C Programming forums, part of the General Programming Boards category; I am currently trying to read in words from text that is seperated by commas here is an example of ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    10

    help with strtok

    I am currently trying to read in words from text that is seperated by commas
    here is an example of one
    snoop,dogg,457890223,black,black,lbc,cali,murder,w as,the,case , , , , , , ,005
    jimmy,crack,476892334,blond,green,paris,tennesee,d ui,jaywalking, , , , , , , , ,003
    I am then trying to strcpy using strtok, and storing in a structure
    Code:
     strcpy(criminal[z].fname, strtok(line,",")); 
    	  strcpy(criminal[z].lname, strtok(NULL,","));
    	  strcpy(criminal[z].SSN, strtok(NULL,","));
          strcpy(criminal[z].hair, strtok(NULL,","));
          strcpy(criminal[z].eyes, strtok(NULL,","));
          strcpy(criminal[z].city, strtok(NULL,","));
          strcpy(criminal[z].state, strtok(NULL,","));
    	  for (x=0;x < 10;x++)
    	  {
    		strcpy(criminal[z].crimes[x], strtok(NULL,","));
            p = strlen(criminal[z].crimes[x]);
                     if(p < 2)
                     {
                     criminal[z].amount = (x-1);
                     
    				 }
    	  }
           strcpy(criminal[z].ranking, strtok(NULL,","));
    	  printf("criminal[%d].ranking = %s",z,criminal[z].ranking);
    everything works beautifully except one thing
    When I display my contents, the first word of the next line is placed becomes associated with the criminal[1].fname, as well as, being added to the end of criminal[0].ranking

    I assume it is because I am using the strtok NULL, but what else could I use that would stop at the end of a line?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    Code:
    rdfile = fopen("FBI.txt","r");
    
         if(rdfile == NULL)
         {
         printf("\nfile could not be opened");
         
    	 }
         else
         {
          while(DONE != '\0')
          {
    		
          DONE = fgets(line,100,rdfile);
    
          if(DONE != '\0')
          {
    
    
    		
          /*new lines of code for dynamic memory allocation*/
          z++;
          criminal = (FBI*)realloc(criminal,(z+1)*sizeof(FBI));
          		
          //actually reads in from text
          strcpy(criminal[z].fname, strtok(line,",")); 
    	  strcpy(criminal[z].lname, strtok(NULL,","));
    	  strcpy(criminal[z].SSN, strtok(NULL,","));
          strcpy(criminal[z].hair, strtok(NULL,","));
          strcpy(criminal[z].eyes, strtok(NULL,","));
          strcpy(criminal[z].city, strtok(NULL,","));
          strcpy(criminal[z].state, strtok(NULL,","));
    	  for (x=0;x < 10;x++)
    	  {
    		strcpy(criminal[z].crimes[x], strtok(NULL,","));
            p = strlen(criminal[z].crimes[x]);
                     if(p < 2)
                     {
                     criminal[z].amount = (x-1);
                     
    				 }
    	  }
           strcpy(criminal[z].ranking, strtok(NULL,","));
    	  printf("criminal[%d].ranking = %s",z,criminal[z].ranking);
    	  counter++;
          }}}//end while//
    printf("criminal[0].ranking = %s",criminal[0].ranking);
          fclose(rdfile);
    I think the problem is with that last strcpy, but I can't seem to figure out what else I could use in its place. Because it reads until the next comma which comes after the next first name.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    hello?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    >hello ?
    We all live in different time zones!

    >z++;

    What is z being initialised to ! if 0 then z is going to equal 1 when criminal[z] is being updated on the first loop, so criminal[0] will never be updated.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    thanks a bunch for your help. I have one more problem though
    when adding an additional criminal, when I write to file I have an endless loop somewhere, if it is apparent somewhere in this code, I would appreciate your help, if not I understand
    by the way z is initialized to -1
    z is being passed to view as num
    and (z+1) is being passed to add as num

    Code:
    void Add(FBI *criminal,int num)//changed counter to num
    {
    	
    	if (num >= 1000)
    	{
    		printf("Sorry, you have reached the maximum capacity of entries\n");
    	}
    	else
    	{
    
    
    		printf("\nPlease enter the first name of the criminal you wish to add:");
    		fflush(stdin);
    		scanf("%s",criminal[num].fname);
    		printf("\nPlease enter the last name of the criminal you wish to add:");
    		fflush(stdin);
    		scanf("%s",criminal[num].lname);
    		printf("\nNow enter the Social Security number:");
    		fflush(stdin);
    		scanf("%s",criminal[num].SSN);
    		printf("\nNow enter the criminal's hair color:");
    		fflush(stdin);
    		scanf("%s",criminal[num].hair);	
    		printf("\nNow enter the criminal's eye color:");
    		fflush(stdin);
    		scanf("%s",criminal[num].eyes);
            printf("\nNow enter the criminal's city:");
    		fflush(stdin);
    		scanf("%s",criminal[num].city);
            printf("\nNow enter the criminal's state:");
    		fflush(stdin);
    		scanf("%s",criminal[num].state);
    		printf("\nNow enter the last ten crimes committed by the criminal:");
    		printf("\nHit enter after each crime");
    		printf("\nType q when done.       ");
    		for (x = 0; x < 10; x++)
    		{
    			fflush(stdin);
    			scanf("%s",criminal[num].crimes[x]);
    			criminal[num].amount = x+1;
    			if (criminal[num].crimes[x][0] == 'q') 
                {
    				criminal[num].amount = x;
    				criminal[num].crimes[x][0] = '\0';
    				x = 10;
                }
    			printf("			");
            }
    		printf("\nNow enter the criminal's ranking (three digits: 001 -for number 1) among the FBI's most wanted list:");
    		fflush(stdin);
    		scanf("%s",criminal[num].ranking);
    		system("cls");
    	}/*end else*/
    }/*end ADD*/
    Code:
    //writes to file//
                            wrfile = fopen("FBI.txt","w");
                             if(wrfile == NULL)
                             {
                             printf("\nfile could not be opened");
                             }
                             else
    	                         {
                                 for (x = 0; x < counter; x++)
                                 {
    								printf("\nx = %d, counter = %d",x, counter);
    								
    								fprintf(wrfile,"%s,%s,%s,%s,%s,%s,%s,",criminal[x].fname, criminal[x].lname, criminal[x].SSN,
          							criminal[x].hair, criminal[x].eyes, criminal[x].city, criminal[x].state);
                                    count10 = 0;
    								
                                         for (y = 0; y < (criminal[x].amount-1); y++)
    								{
    									fprintf(wrfile,"%s,",criminal[x].crimes[y]);
    									count10++;
    								}
    								fprintf(wrfile,"%s",criminal[x].crimes[y]);
    								for (b = 0; b <= (10 - criminal[x].amount); b++)
    								{
    									fprintf(wrfile," ,");
    								}
    								fprintf(wrfile,"%s ",criminal[x].ranking);
    								fprintf(wrfile,"\n");
    		
    							 }//end for//
                              }//end else//
                                  fclose(wrfile);

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    fflush(stdin);
    Stop flushing input streams. It's wrong.

    Code:
    for (y = 0; y < (criminal[x].amount-1); y++)
    {
        fprintf(wrfile,"%s,",criminal[x].crimes[y]);
        count10++;
    }
    Why don't you just put count10 in your for loop parameters? There's no need for it to be out of it.

    thanks a bunch for your help. I have one more problem though
    when adding an additional criminal, when I write to file I have an endless loop somewhere, if it is apparent somewhere in this code, I would appreciate your help, if not I understand
    by the way z is initialized to -1
    z is being passed to view as num
    and (z+1) is being passed to add as num
    The variable "z" is not used in your write code.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM

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