简体   繁体   English

从不同的文件读取并在字符串上使用strtok

[英]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 ." 所以这是一个从2个不同的文件(第一行//第二行)中读取3个字符串(orig // test1 // orig_copy)的代码, 并调用divid_string使用strtok并获取令牌并将其存储在**(token_orig // token_test中) // token_orig_copy)->这就是问题所在: -当我将三行放在main中时,它会编译并从所有3个字符串和“ 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 -但是当我尝试接下来的三行时(注意我如何将“ HAHAHAH ”更改为“ HAHAHAHA ”,那一点点的更改都会改变一切并使程序在printf(“字符串二:”)处停止;我希望我清除了问题PS:您可以过去复制程序,以便轻松编译自己

    #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! 由于令牌是指针,因此sizeof(token)会为您提供指针变量的大小(可能为4或8个字节),而不是它所指向的字符串中的字符数! You want: 你要:

strlen(token) + 1

instead (+1 for the \\0). 而是(\\ 0为+1)。

About the only time sizeof is useful for character strings is literals like: sizeof对字符串有用的唯一时间是字面量,例如:

sizeof("Hello World")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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