Tag: strtok

从文本文件中读取一行,并在struct中放置由空格分隔的数据

我要在C中编写一个函数,它将filepointer作为输入并返回如下结构: typedef struct product{ char * code_product; char * name; char * code_piece; only_time_t enter; only_time_t exit; }product_t; 这个结构使用另一个这样的结构: typedef struct only_time{ int hour; int minute; int second; }only_time_t; 为了从文件中读取,我使用了getline()函数,我使用strtok()函数来创建令牌。 这是我用来从文件中读取的函数: product_t * read_line(FILE * fp){ char * line = NULL; size_t len = 0; product_t * temp; int i = 0; temp = (product_t […]

基于空格或“双引号字符串”将字符串解析为数组

我试图取一个用户输入字符串并解析为一个名为char * whole_line [100]的数组; 其中每个单词放在数组的不同索引处但如果字符串的一部分由引号封装,则应将其放在单个索引中。 所以,如果我有 char buffer[1024]={0,}; fgets(buffer, 1024, stdin); 示例输入:“word filename.txt”这是一个字符串,shoudl占用输出数组中的一个索引“; tokenizer=strtok(buffer,” “);//break up by spaces do{ if(strchr(tokenizer,'”‘)){//check is a word starts with a ” is_string=YES; entire_line[i]=tokenizer;// if so, put that word into current index tokenizer=strtok(NULL,”\””); //should get rest of string until end ” strcat(entire_line[i],tokenizer); //append the two together, ill take care of […]

C:警告:赋值从整数中生成没有强制转换的指针

这是我的代码 #include #include void main() { FILE *fp; char * word; char line[255]; fp=fopen(“input.txt”,”r”); while(fgets(line,255,fp)){ word=strtok(line,” “); while(word){ printf(“%s”,word); word=strtok(NULL,” “); } } } 这是我得到的警告。 token.c:10:7: warning: assignment makes pointer from integer without a cast [enabled by default] word=strtok(line,” “); ^ token.c:13:8: warning: assignment makes pointer from integer without a cast [enabled by default] word=strtok(NULL,” […]

使用strtok在C中解析字符串

我有这个小源代码,用于测试解析类似于我需要在其他项目中使用的变量string #include #include #include int main (void) { char string[] = “C-AC-2C-3C-BOB”; char* s; char* hand[3]; char* usr; s = (char*) calloc(1, sizeof(char)); hand[1] = (char*) calloc(3, sizeof(char)); hand[2] = (char*) calloc(3, sizeof(char)); hand[3] = (char*) calloc(3, sizeof(char)); usr = (char*) calloc(21, sizeof(char)); s = strtok (string,”-“); hand[1] = strtok (NULL, “-“); hand[2] = strtok […]

从递归返回后,c strtok返回NULL

当我没有在我的代码中调用相同的函数时一切正常但当函数突然返回时,变量pch为NULL: void someFunction() { char * pch; char tempDependencies[100*64+100]; strcpy(tempDependencies,map[j].filesNeeded); pch = strtok(tempDependencies,”,”); while (pch != NULL) { someFunction(); <- if i comment this out it works fine pch = strtok (NULL, ","); } } 因此,例如当循环作用于字符串file2,file3,file4它正确地分割file2并将字符串修改为file2\\000file3,file4但是下一次调用pch = strtok (NULL, “,”); 将pch渲染为0x0 。 在调用递归时是否有我不知道的事情?

在C中双重拆分

好。 例如,我在我的txt文件中有这一行: 1|1,12;7,19;6,4;8,19;2,2 如您所见,它有两部分,以|分隔 。 我没有问题得到两个部分,并分离第二部分1,12;7,19;6,4;8,19;2,2使用; 分隔器。 但我确实有进一步分离的问题,获得每组的第一和第二个数字。 这是我目前的代码: result = strtok(result, “;”); while(result != NULL ) { printf(“%s\n”, result); result = strtok(NULL, “;”); } 它输出我: 1,12 7,19 6,4 8,19 2,2 太好了。 但是,当我尝试“strtok”(我正在使用此方法进行拆分)时,如下所示: result = strtok(result, “;”); while(result != NULL ) { //printf(“%s\n”, result); help = strtok(result, “,”); while(help != NULL) { printf(“”, help); help […]

当一行中有2个分隔符时,将字符串拆分为C中的标记

我正在使用strtok()函数将字符串拆分为Tokens。问题是当行中有2个分隔符时。 /* strtok example */ #include #include int main () { char str[] =”Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,,”; char * pch; printf (“Splitting string \”%s\” into tokens:\n”,str); pch = strtok (str,”, “); while (pch != NULL) { printf (“Token = %s\n”,pch); pch = strtok (NULL, “, “); } return 0; } 并输出: Splitting string “Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,,” into tokens: […]

使用strtok读取csv文件

我试图在C中使用strtok来读取csv文件,并将内容存储到struct Game的数组中。 我的代码如下所示: FILE *fp; int i = 0; if((fp=fopen(“Games.csv”,”r”))==NULL) { printf(“Can’t open file.\n”); exit(1); } rewind(fp); char buff[1024]; fgets(buff,1024,fp); char* delimiter = “,”; while(fgets(buff, 1024, (FILE*)fp)!=NULL && i<5){ Game[i].ProductID= strtok(buff, ","); Game[i].ProductName = strtok(NULL, delimiter); Game[i].Publisher = strtok(NULL, delimiter); Game[i].Genre = strtok(NULL, delimiter); Game[i].Taxable = atoi(strtok(NULL, delimiter)); Game[i].price = strtok(NULL, delimiter); Game[i].Quantity = atoi(strtok(NULL, […]

无法连接strtok的输出变量。 strcat和strtok

我已经花了几个小时在这个程序上,并花了几个小时在线搜索我的方法的替代品,并在整个晚上一直困扰着崩溃和错误…… 我有一些我想用这段代码实现的东西。 首先,我将解释我的问题,然后我将发布代码,最后我将解释我对该程序的需求。 程序只输出单个单词,连接函数什么都不做。 这似乎应该足够简单来修复…… 我的第一个问题是我似乎无法使concatenate函数工作,我使用了通用的strcat函数,它没有工作,也没有我在互联网上找到的另一个版本(这个函数在这里使用,它被称为“mystrcat” )。 我希望用字符串读取程序并删除“分隔符”以创建由原始字符串中的每个单词组成的单个字符串。 我正在尝试使用strtok和strcat函数。 如果有一个更简单或更简单的方式请尽我所有的耳朵。 另一个问题,不一定是一个问题,而是一个丑陋的混乱:主要的七行。 我更喜欢按如下方式初始化变量:char variable [amt]; 但我发现strtok的代码是使用指针,而strcat函数的代码是使用指针。 更好地理解字符串的指针和地址可能会帮助我长期。 但是,我想以任何必要的方式摆脱其中的一些线索。 我不能只有2行专用于2个变量。 当我有10个变量时,我不想要30行顶部… #include #include #include char *mystrcat(char *output, char *firstptr); int main() { char str[] = “now # is the time for all # good men to come to the # aid of their country”; char delims[] = […]

与PHP在C中的“爆炸”并行:使用分隔符将char *拆分为char *

可能重复: 在C中拆分带分隔符的字符串 我正在寻找一种使用分隔符将char * “爆炸”成其他char *的好方法。 我的分隔符将是#