Thread: strtok error help - C

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    1

    strtok error help - C

    the text file has two lines :


    2012-01-01,12:01,San Jose,Music,12.99,Amex
    2012-01-02,10:00,Santa Clara,Toys,45.00,Visa




    i need to group each line into separate arrays


    like, date[0] = 2012-01-01
    date[1] = 2012-01-02


    time[0] = 12:01
    time[1] = 10:00 and so on for other elements also


    i have implemented the logic, its working for dates, but not for others, don't know why can u please check and see if u could solve it.

    } }


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>  //for strtok
    
    #define PATH "C:\\Users\\Padma\\Desktop\\sales.txt"
    
    struct sales{
    
    	char Dates[50];
    	char time[50];  //hrsec
       	char storename[50];    //char *strs[NUMBER_OF_STRINGS] = {"foo", "bar", "bletch", ...};, for store name
       	char item[50];  //NAMES OF ITEMS IN STORES
       	char cost[50];
       	char payment[50];
    };
    
    
    	struct sales sales1[50];
    int main(){	
    
    	FILE* file;
    	
    	file = fopen(PATH,"r");
    	int i = 0;
    	
    	const char *s = ",";
      	char *token = NULL;
      	
      	
    	
       	
       	char line[1000];  //no of characters in each line
       	
       	
       		while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ {
    			printf("%s\n\n",line); //print the file contents on stdout.
    			
    			token = strtok(line, s);
    			strcpy(sales1[i].Dates,token);
    			printf("%s",sales1[0].Dates);
    		//	while(token != NULL){
           		
           			token=strtok(NULL,s);
           			strcpy(sales1[i].time,token);
           			
           			token = strtok(NULL,s);
        			strcpy(sales1[i].storename,token);
        			
        			token = strtok(NULL,s);
        			strcpy(sales1[i].item,token);
        			
        			token = strtok(NULL,s);
        			strcpy(sales1[i].cost,token);
        			
        			token = strtok(NULL,s);
        			strcpy(sales1[i].payment,token);
           			i++;
           			//break;
         	//}
    				
    				
    		   
    		}
    		int y =0;
    	for(y = 0; y < 2; y++){
            printf("%s ", sales1[y].Dates);
            printf("\n%s", sales1[y].time);
    
    }
    	
    }
    Attached Files Attached Files
    Last edited by Salem; 11-28-2014 at 04:31 PM. Reason: inlined the code

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by padma View Post
    the text file has two lines :


    2012-01-01,12:01,San Jose,Music,12.99,Amex
    2012-01-02,10:00,Santa Clara,Toys,45.00,Visa




    i need to group each line into separate arrays


    like, date[0] = 2012-01-01
    date[1] = 2012-01-02


    time[0] = 12:01
    time[1] = 10:00 and so on for other elements also


    i have implemented the logic, its working for dates, but not for others, don't know why can u please check and see if u could solve it.

    Here you speak over arraies, but in the code later, you use structures.
    Quote Originally Posted by padma View Post
    Code:
    […]
        struct sales sales1[50];
    
    int main(){    
    […]
    Why is 'sales1' global? - It is not necessary.
    How many arguments take the main-function? - What i can see … none. Remember, empty parentheses at function declaration/definition are allways bad practice.
    After the main should return an int-value, set an appropriate return statement at end of code (i.e. return EXIT_SUCCESS).
    Fixit:
    Code:
    […]
    int main(void) {
    
        struct sales sales1[50];
    […]
    Quote Originally Posted by padma View Post
    Code:
    […]
        const char *s = ",";
    […]
                token = strtok(line, s);
    […]
    You don't need define a char for strtok.
    For this little code, you can give the character directly to strtok.
    Code:
                token = strtok(line, ",");
    If you write bigger programs, you can define is like:
    Code:
    #define TOKEN ","
    
                token = strtok(line, TOKEN);
    Quote Originally Posted by padma View Post
    Code:
    […]
                    token=strtok(NULL,s);
                    strcpy(sales1[i].time,token);
    
                    token = strtok(NULL,s);
                    strcpy(sales1[i].storename,token);
    
                    token = strtok(NULL,s);
                    strcpy(sales1[i].item,token);
    
                    token = strtok(NULL,s);
                    strcpy(sales1[i].cost,token);
    
                    token = strtok(NULL,s);
                    strcpy(sales1[i].payment,token);
    […]
    It will be more secure, if you check that there is a pointer returned by strtok.
    Like:
    Code:
                    if ((token = strtok(NULL, ",")))
                        strcpy(sales1[i].time,token);
                    else {
                        fprintf(stderr, "no token left! exit.\n");
                        return EXIT_FAILURE;
                    }
    Incidently, you don't check if the file was successfully open. Also, you don't close the file.
    If you have an trailing newline in your file (sales.txt), 'fgets' will read as a line, but there is only a newline.
    A good solution is to check the length of line and all token commands goes in the body of that if scope (inclusively the 'i++').
    With this solution, you jump automaticly over empty lines.
    You give out the Date after you copy the token, but you print allways the Date from sales1[0] insteed of sales1[i].
    Last edited by WoodSTokk; 11-28-2014 at 06:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok(), strdup() error
    By Adam Rinkleff in forum C Programming
    Replies: 4
    Last Post: 07-31-2011, 08:24 PM
  2. Help using strtok
    By HIT_Braga in forum C Programming
    Replies: 5
    Last Post: 03-30-2010, 06:28 AM
  3. strtok Error
    By it01y2 in forum C Programming
    Replies: 2
    Last Post: 01-18-2010, 12:14 PM
  4. strtok
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 07:44 PM
  5. strtok & gets
    By sketchit in forum C Programming
    Replies: 1
    Last Post: 09-29-2001, 02:16 PM

Tags for this Thread