Tag: 拆分

在C中拆分未加引号的字符串

我正在编写一个函数将字符串拆分为指向指针的指针,如果separator是空格,我想只拆分不在引号内的单词。 例如Hello world “not split”应该返回 Hello world “not split” 不知何故,该函数将引号内的单词拆分,并且不会在引号之外拆分单词。 #include #include #include int is_quotes(char *s) { int i; int count; i = 0; count = 0; while (s[i]) { if (s[i] == ‘”‘) count++; i++; } if (count == 0) count = 1; return (count % 2); } int count_words(char *s, char sep) { […]

将char *拆分为char * Array

我正在尝试将char *拆分为C中的char *数组。我习惯用Java / PHP OO编程。 我知道在这些语言中有几种简单的方法,但在C语言中……我完全迷失了。 我经常有几个小时的段错误x) 我正在使用TinyXML并从XML文件中获取信息。 这是我们找到数组的结构。 const int MAX_GATES = 64; typedef struct { char *name; char *firstname; char *date; char *id; char *gates[MAX_GATES]; } UserInfos; 这是我填充此结构的地方: UserInfos * infos = (UserInfos*)malloc(1024); infos->firstname = (char*)malloc(256); infos->name = (char*)malloc(128); infos->id = (char*)malloc(128); infos->date = (char*)malloc(128); sprintf(infos->firstname, “%s”, card->FirstChild(“firstname”)->FirstChild()->Value()); sprintf(infos->name, “%s”, card->FirstChild(“name”)->FirstChild()->Value()); sprintf(infos->date, […]

在C中的#中拆分字符串

我有一个像这样的字符串: char *message = “12#34#56#78#90” 我想得到: a = “12” b = “34” c = “56” d = “78” d = “90” 谁能给我一个好方法?

用C读取文件并拆分字符串

我有一个程序在C中读取文件。现在我想把字符串除以空格分成一个数组。 我该怎么做? #include int main() { char line[30]; char names[100][20]; int sizes[100]; int i = 0; FILE *fp; fp = fopen(“in.txt”, “rt”); if(fp == NULL) { printf(“cannot open file\n”); return 0; } while(fgets(line, sizeof(line), fp) != NULL) { printf(line); i++; } fclose(fp); return 0; }

如何用C中的另一个字符串替换一段const char *?

假设您有一个类似http://1.1.1.1/test.mpg的链接。 然后你想把它改成http://1.1.1.1/test.mkv 。 如何在C中以编程方式将“mpg”更改为“mkv”? 我尝试使用strtok和strcpy,但我不擅长C,所以我不能这样做。

如何在C中将数组拆分为两个数组

假设我在C中有一个数组 int array[6] = {1,2,3,4,5,6} 我怎么能把它分成两部分 {1,2,3} 和 {4,5,6} 使用memcpy可以实现吗? 谢谢, 不不不

从套接字缓冲区逐行读取

好吧,我需要帮助:我想写一个函数,从一个从unistd.h头的read()函数的第三个参数获得的套接字缓冲区中逐行read() 。 我写了这个: int sgetline(int fd, char ** out) { int buf_size = 128; int bytesloaded = 0; char buf[2]; char * buffer = malloc(buf_size); char * newbuf; int size = 0; assert(NULL != buffer); while( read(fd, buf, 1) > 0 ) { strcat(buffer, buf); buf[1] = ‘\0’; bytesloaded += strlen(buf); size = size + […]

有没有办法在C中的多个字符上拆分字符串?

在C中是否有一种方法来分割字符串(使用strtok或任何其他方式),其中分隔符的长度超过一个字符? 我正在寻找这样的东西: char a[14] = “Hello,World!”; char *b[2]; b[0] = strtok(a, “, “); b[1] = strtok(NULL, “, “); 我希望这不会拆分字符串,因为逗号和W之间没有空格。有没有办法做到这一点?

如何在C中将字符串拆分为标记?

如何在C中用’&’将字符串拆分为标记?