简体   繁体   中英

reading from different files and using strtok on strings

so this is a code that reads 3 strings (orig // test1 // orig_copy) from 2 different files (firstline // secondline)**and calls divide_string to use strtok and take tokens and store them in **(token_orig // token_test // token_orig_copy) , --> this is the problem : - when i put the three lines in main it does compile and take token from all 3 strings and "Done ." in the end. -but when i try the next three lines (notice how i changed " HAHAHAH " to " HAHAHAHA ", that little changing changes everything and make the program stops at printf("for the string number two :"); . i hope i cleared the problem PS : you can past copy the program so you can compile yourself easily

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char s[4] = " ,.";
int divide_string(char* thestring,char** destination)
{
    int i=0;
     char* token=strtok(thestring,s);
     destination[i]=malloc(sizeof(token)+1);
     strcpy(destination[i],token);
     i++;
     printf("the word %d is 'tokened' \n",i);
     while(token!=NULL)
     {
              token =strtok(NULL,s);
         if (token != NULL)
         {
             destination[i]=malloc(sizeof(token)+1);
             strcpy(destination[i],token);
             printf("the word %d is 'tokened' \n",i);
             ++i;
         }
     }
     return i;
}
void main ()
{ //TRY THESE THREE LINES THAT WORKS<-----------------------------
 char orig[]= "does work HAHAHAH";
char orig_copy[] = "does work HAHAHAH";
char test1[]="does work HAHAHAH";
//    char orig[]= "doesnt work HAHAHAHA";
//    char orig_copy[] = "doesnt work HAHAHAHA";
//    char test1[]="doesnt work HAHAHAHA";
    char *token_orig[81];
char *token_test[81];
char *token_orig_copy[81];
strcpy(orig_copy,orig);
printf("for string number one : \n");
int max_orig = divide_string(orig,token_orig);
printf("for string number two : \n");
int a        = divide_string(orig_copy,token_orig_copy);
printf("for string number three : \n");
int max_test = divide_string(test1,token_test);
printf("%s-",token_orig[0]);
printf("%s-",token_orig[1]);
printf("%s-\n",token_orig[2]);
printf("%s-",token_orig_copy[0]);
printf("%s-",token_orig_copy[1]);
printf("%s-\n",token_orig_copy[2]);
printf("%s-",token_test[0]);
printf("%s-",token_test[1]);
printf("%s-\n",token_test[2]);
    printf("done .");
return 0;
}

Since token is a pointer, sizeof(token) gives you the size of the pointer variable (4 or 8 bytes probably), NOT the number of chars in the string it points to! You want:

strlen(token) + 1

instead (+1 for the \\0).

About the only time sizeof is useful for character strings is literals like:

sizeof("Hello World")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM