fscanf怎么获取字符串?

happyboy16 2009-12-29 08:24:17
一个处理字符串的程序
需要处理如下字符串:32275,sr,1,Qs,28135,56,94
第二段,和第四段是字符串,长度不定,其他都是整数型,如下:
32275,sr,1,Qs,28135,56,94
32275,fthg,1,rty,28135,56,94
32275,asc,1,gs,28135,56,94
32275,njk,1,nyagd,28135,56,94


int pid,di,a,b,c;
char uid[10];ch[10];
我使用fgets获取一行,然后用fscanf("%d,%c,%d,%c,%d,%d",&pid,&uid,&di,&ch,&a,&b,&c)分别赋给每个值,这一步一直有问题,请告诉赐教,问题出在哪里?谢谢!
...全文
828 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cattycat 2009-12-30
  • 打赏
  • 举报
回复
sscanf(buf,"%d,%[^,],%d,%[^,],%d,%d,%d",&pid,uid,&di,ch,&a,&b,&c);

这个倒是很好用,其实就是getline后,用sscanf来处理。
selooloo 2009-12-30
  • 打赏
  • 举报
回复
用正则表达式


#include <stdio.h>

int main(void)
{
char *buf="32275,sr,1,Qs,28135,56,94 ";
int pid,di,a,b,c;
char uid[10],ch[10];

sscanf(buf,"%d,%[^,],%d,%[^,],%d,%d,%d",&pid,uid,&di,ch,&a,&b,&c);
printf("%d,%s,%d,%s,%d,%d,%d",pid,uid,di,ch,a,b,c);
getchar();
return 0;
}
xianyuxiaoqiang 2009-12-30
  • 打赏
  • 举报
回复
%s不会遇到','就截断,而是把后续字符串全部获取,所以,对于字符串部分,需要自己实现分割。
%c只处理单个字符,无法获取一批字符。
xianyuxiaoqiang 2009-12-30
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#define MAXWORD 1000
void teststring(char*str){
int a,b,c,d,e;
char buffer[MAXWORD];
char buffer2[MAXWORD];
char str1[MAXWORD];
char str2[MAXWORD];
int length = 0;
int i = 0;
int buflen = 0;

if(NULL == str)return;
length = strlen(str);
if(length >= MAXWORD)return;
/* get first int */
sscanf(str,"%d,%s", &a, buffer);
buflen = strlen(buffer);
/* get the ',' after the first string */
for(i = 0; i < buflen; i++){
if(buffer[i] == ','){
break;
}
}
if(i >= buflen)return;/* invalid input */
buffer[i] = 0;/* cut the ',' to string ending */
strcpy(str1, buffer);

/* get the second int */
sscanf(buffer+i+1,"%d,%s", &b, buffer2);
buflen = strlen(buffer2);
/* get the ',' after the second string */
for(i = 0; i < buflen; i++){
if(buffer2[i] == ','){
break;
}
}
if(i >= buflen)return;/* invalid input */
buffer2[i] = 0;/* cut the ',' to string ending */
strcpy(str2, buffer2);

/* get rest int */
sscanf(buffer2+i+1,"%d,%d,%d", &c, &d, &e);

printf("%d\n%s\n%d\n%s\n%d\n%d\n%d\n", a, str1, b, str2, c, d, e);
}
void main(){
teststring("32275,srasdasd,123131,Qsasdasda,28135,51236,94123");
return;
}
  • 打赏
  • 举报
回复
呃。已经被抢答了。。。
happyboy16 2009-12-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lyboy_caidou 的回复:]
引用 1 楼 haozi8993 的回复:
Assembly codefscanf("%d,%s,%d,%s,%d,%d",&pid,&uid,&di,&ch,&a,&b,&c)这样行不?

fscanf("%d,%s,%d,%s,%d,%d,%d",&pid,uid,&di,ch,&a,&b,&c)
少了个%d,且字符数组名就是首地址!不用加&
[/Quote]
没加&,sscanf("%d,%s,%d,%s,%d,%d,%d",&pid,uid,&di,ch,&a,&b,&c),还是不能正常获取值
happyboy16 2009-12-29
  • 打赏
  • 举报
回复
我用fgets获取一行到buffer中,居然获取不了~~
有些文件我用fgets可以很好的获取,但是这个文件不知道为什么fgets之后buffer没有被赋值
lyboy_caidou 2009-12-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 haozi8993 的回复:]
Assembly codefscanf("%d,%s,%d,%s,%d,%d",&pid,&uid,&di,&ch,&a,&b,&c)这样行不?
[/Quote]
fscanf("%d,%s,%d,%s,%d,%d,%d",&pid,uid,&di,ch,&a,&b,&c)
少了个%d,且字符数组名就是首地址!不用加&
  • 打赏
  • 举报
回复
32275,njk,1,nyagd,28135,56,94

fscanf("%d,%c,%d,%c,%d,%d %d",

你少了个%d吧,这样无法一行行正确读取。
考虑先getline,再用sscanf处理吧。
罗耗子 2009-12-29
  • 打赏
  • 举报
回复
fscanf("%d,%s,%d,%s,%d,%d",&pid,&uid,&di,&ch,&a,&b,&c)
这样行不?

70,039

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧