problems with strtok()

This is a discussion on problems with strtok() within the C Programming forums, part of the General Programming Boards category; Right now I'm having a problem using the strtok() function, it's giving a segmentation fault for some reason and I ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    27

    problems with strtok()

    Right now I'm having a problem using the strtok() function, it's giving a segmentation fault for some reason and I don't know why.

    [code]
    curr = strtok(file[0],",");

    while(1){
    curr = strtok(NULL,",");
    if(curr == NULL) break;
    }

    [\code]

    file is a 2d array of characters, which is created with the fgets function as below:

    Code:
    while(fgets(c,1000,fptr) != NULL){
      strcpy(file[i++],c);
    }
    I'm able to get to the last entry in the string (I'm parsing a CSV file) however I get a seg fault after that. I have a feeling that it's because of the newline character at the end of the string using fgets. So I created a function to strip the new line character however my strtok() function no longer works.

    My stripnewline function:
    Code:
    void stripnew(char * string){
    	if(string[strlen(string) - 1] == '\n') string[strlen(string) - 1] = '\0';
    }
    If anybody could suggest a solution to this problem much would be appreciated.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Right now I'm having a problem using the strtok() function, it's giving a segmentation fault for some reason and I don't know why.
    Neither do I. Nothing about what you've posted is suspect. Well, except for strcpy. That may be the source of the problem. But it's impossible to actually diagnose with this small amount of code.

    You really need to post a small, compilable example that demonstrates the problem. If your program is small enough, just dump it here, so I can see what your pointers are actually doing.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    27
    Here is my entire code:

    Code:
    void stripnew(char *);
    
    int main(void){
    	FILE * fptr;
    	char c[1000];
    	char ** file;
    	int * deleteArray;
    	int i;
    	int numentries = 0;
    	char * curr;
    	int count;
    	fptr = fopen(FILENAME,"r");
    	if(fptr == NULL){
    		printf("Unable to Open file anncomp.csv!\n");
    		exit(1);
    	}
    	
    	while(fgets(c,1000,fptr) != NULL){
    		//printf("%s",c);
    		numentries++;
    	}
    	printf("numentries = %d\n",numentries);
    	file = (char **) malloc(sizeof(char*) * numentries);
    	for(i = 0;i < numentries;i++) file[i] = (char *) malloc(sizeof(char) * 1000);
    	
    	rewind(fptr);
    	i = 0;
    	while(fgets(c,1000,fptr) != NULL){
    	  strcpy(file[i++],c);
    	}
    	
    	deleteArray = (int *)malloc(sizeof(int) * numentries);
    	
    	for(i=0;i<numentries;i++) deleteArray[i] = 0;
    	//for(i=0;i<numentries;i++){
    	//	printf("%s\n",file[i]);
    	//}
    		
    	//for(i=0;i<numentries;i++){
    		//stripnew(file[0]);
    	    printf("%s",file[0]);
    		curr = strtok(file[0],",");
    		printf("%s",curr);
    		
    		count = 1;
    		
    		while(1){
    		//    if(count == 44) break;
    			curr = strtok(NULL,",");
    			if(curr == NULL) break;
    			printf("%s\n",curr);
    		//	count++;
    		}
    		
    		if(strcmp(curr,"") == 0) deleteArray[i] = 1;
    	//}
    	
    	//for(i=0;i<numentries;i++) printf("%d",deleteArray[i]);
    	
    	
    	return 0;
    }
    
    void stripnew(char * string){
    	if(string[strlen(string) - 1] == '\n') string[strlen(string) - 1] = '\0';
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    if(strcmp(curr,"") == 0) deleteArray[i] = 1;
    When you reach this line, curr is guaranteed to be NULL....

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. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  3. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  4. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM
  5. Problems printing a linked list.
    By Ed_Severson in forum C Programming
    Replies: 4
    Last Post: 05-03-2002, 10:31 PM

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