user-0 提问: C语言从一个字符串中提取指定的几个字符串并打印出来的问题

回复:
user: user-1
content: 引用 2 楼 zhao4zhong1 的回复:作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。  #include <stdio.h> char buf[1024] = "[{\"bucket_name\":\"andy-personal-cloud4\",\"status\":\"0\",\"cdatetime\":\"1402481705\",\"used_capacity\":\"0\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud3\",\"status\":\"0\",\"cdatetime\":\"1402479952\",\"used_capacity\":\"0\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud2\",\"status\":\"0\",\"cdatetime\":\"1402386155\",\"used_capacity\":\"119056\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud\",\"status\":\"0\",\"cdatetime\":\"1401209441\",\"used_capacity\":\"19491722\",\"total_capacity\":\"0\",\"region\":\"beijing\"}]..."; char s[40]; char *p; int n,r; int main() {     p=buf;     while (1) {         r=sscanf(p,"{\"bucket_name\":\"%39[^\"]\"%n",s,&n);         if (1==r) {             printf("[%s]\n",s);             p+=n;         } else if (0==r) p++;         else break;     }     return 0; } //[andy-personal-cloud4] //[andy-personal-cloud3] //[andy-personal-cloud2] //[andy-personal-cloud] //   +1

user: user-2
content: 6楼代码不如2楼代码简洁。

user: user-3
content: #include <stdio.h> #include <string.h>  char str[1024]="[{\"bucket_name\":\"andy-personal-cloud4\",\"bucket_name\":\"andy-personal-cloud5\",\"bucket_name\":\"andy-personal-cloud6\"}]";  char dst[30]="";  char check[20]="\"bucket_name\":";  int main() { 	char *p=str; 	while((p = strstr(p,check)) != NULL){ 		puts(p); 		sscanf(p,"\"bucket_name\":\"%[^\"]", dst); 		p = p + strlen(check) + strlen(dst)+ 3; 		puts(dst); 	} }

user: user-4
content: 引用 2 楼 zhao4zhong1 的回复:作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。  #include <stdio.h> char buf[1024] = "[{\"bucket_name\":\"andy-personal-cloud4\",\"status\":\"0\",\"cdatetime\":\"1402481705\",\"used_capacity\":\"0\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud3\",\"status\":\"0\",\"cdatetime\":\"1402479952\",\"used_capacity\":\"0\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud2\",\"status\":\"0\",\"cdatetime\":\"1402386155\",\"used_capacity\":\"119056\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud\",\"status\":\"0\",\"cdatetime\":\"1401209441\",\"used_capacity\":\"19491722\",\"total_capacity\":\"0\",\"region\":\"beijing\"}]..."; char s[40]; char *p; int n,r; int main() {     p=buf;     while (1) {         r=sscanf(p,"{\"bucket_name\":\"%39[^\"]\"%n",s,&n);         if (1==r) {             printf("[%s]\n",s);             p+=n;         } else if (0==r) p++;         else break;     }     return 0; } //[andy-personal-cloud4] //[andy-personal-cloud3] //[andy-personal-cloud2] //[andy-personal-cloud] //  ++

user: user-2

user: user-5
content: 引用 2 楼 zhao4zhong1 的回复:作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。  #include <stdio.h> char buf[1024] = "[{\"bucket_name\":\"andy-personal-cloud4\",\"status\":\"0\",\"cdatetime\":\"1402481705\",\"used_capacity\":\"0\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud3\",\"status\":\"0\",\"cdatetime\":\"1402479952\",\"used_capacity\":\"0\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud2\",\"status\":\"0\",\"cdatetime\":\"1402386155\",\"used_capacity\":\"119056\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud\",\"status\":\"0\",\"cdatetime\":\"1401209441\",\"used_capacity\":\"19491722\",\"total_capacity\":\"0\",\"region\":\"beijing\"}]..."; char s[40]; char *p; int n,r; int main() {     p=buf;     while (1) {         r=sscanf(p,"{\"bucket_name\":\"%39[^\"]\"%n",s,&n);         if (1==r) {             printf("[%s]\n",s);             p+=n;         } else if (0==r) p++;         else break;     }     return 0; } //[andy-personal-cloud4] //[andy-personal-cloud3] //[andy-personal-cloud2] //[andy-personal-cloud] //  你太shuai了

user: user-2
content: 作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。  #include <stdio.h> char buf[1024] = "[{\"bucket_name\":\"andy-personal-cloud4\",\"status\":\"0\",\"cdatetime\":\"1402481705\",\"used_capacity\":\"0\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud3\",\"status\":\"0\",\"cdatetime\":\"1402479952\",\"used_capacity\":\"0\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud2\",\"status\":\"0\",\"cdatetime\":\"1402386155\",\"used_capacity\":\"119056\",\"total_capacity\":\"0\",\"region\":\"beijing\"}" ",{\"bucket_name\":\"andy-personal-cloud\",\"status\":\"0\",\"cdatetime\":\"1401209441\",\"used_capacity\":\"19491722\",\"total_capacity\":\"0\",\"region\":\"beijing\"}]..."; char s[40]; char *p; int n,r; int main() {     p=buf;     while (1) {         r=sscanf(p,"{\"bucket_name\":\"%39[^\"]\"%n",s,&n);         if (1==r) {             printf("[%s]\n",s);             p+=n;         } else if (0==r) p++;         else break;     }     return 0; } //[andy-personal-cloud4] //[andy-personal-cloud3] //[andy-personal-cloud2] //[andy-personal-cloud] //

user: user-6
content: 放到std::string里面，然后find