C语言,关于切割这字符串

pressman 2005-06-04 11:06:39
/*我当前代码*/
#include<stdio.h>

struct handinfee
{
char sonbr[20];
char msisdn[11];
char staffid[10];
int fee;
int flag;
}
handinfee[50],*hp;

main()
{
FILE *fp;
char str[1024];
// hp=handinfee;

if((fp=fopen("abc","rt"))==NULL)
{
printf("\nCannot open file strike any key exit!");
exit(1);
}
fgets(str,1024,fp);
printf("%s\n",str);

fclose(fp);
}


/*文件abc内容*/
SONBR=10504277915010130233&MSISDN=XXXX&staff_id=7910000sm2&FEE=3132&FLAG=1;
SONBR=10504297915010130715&MSISDN=XXXX&staff_id=7910000sm2&FEE=0&FLAG=1;
SONBR=10504297915010130716&MSISDN=XXXX&staff_id=7910000sm2&FEE=2000&FLAG=1;
SONBR=10504307915010130873&MSISDN=XXXX&staff_id=7910000sm2&FEE=2000&FLAG=1;
...
/*说明:不会超过50行*/

上面的程序,现在是读文件abc的第一行并放到char str 里了,现在我想把这行里所包含的信息放到struct handinfee 里,但我不知道怎么样才可以正确的切割这个字符串,C不支持字符串类型,我也没找到合适的库函数可以用,所以来这里问问,谢谢了!
...全文
154 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhousqy 2005-06-05
  • 打赏
  • 举报
回复
一个笨的方法:
#include<stdio.h>

struct handinfee
{
char sonbr[100];
char msisdn[11];
char staffid[10];
int fee;
int flag;
} handinfee[50] = { 0 },*hp;

void parseStr(char *str, int i)
{
char *sonbr = "SONBR=",
*msisdn = "MSISDN=",
*staffid = "staff_id=",
*fee = "FEE=",
*flag = "FLAG=";
char *s, *e;

if ((s = strstr(str, sonbr)) != NULL) {
s += strlen(sonbr);
if ((e = strchr(s, '&')) != NULL) {
*e = '\0';
strcpy(handinfee[i].sonbr,s);
s = e + 1;
}
}

if ((s = strstr(s, msisdn)) != NULL) {
s += strlen(msisdn);
if ((e = strchr(s, '&')) != NULL) {
*e = '\0';
strcpy(handinfee[i].msisdn,s);
s = e + 1;
}
}

if ((s = strstr(s, staffid)) != NULL) {
s += strlen(staffid);
if ((e = strchr(s, '&')) != NULL) {
*e = '\0';
strcpy(handinfee[i].staffid,s);
s = e + 1;
}
}

if ((s = strstr(s, fee)) != NULL) {
s += strlen(fee);
if ((e = strchr(s, '&')) != NULL) {
*e = '\0';
handinfee[i].fee = atoi(s);
s = e + 1;
}
}

if ((s = strstr(s, flag)) != NULL) {
s += strlen(flag);
if ((e = strchr(s, ';')) != NULL) {
*e = '\0';
handinfee[i].flag = atoi(s);
s = e + 1;
}
}
}

main()
{
FILE *fp;
char str[1024];
int i, j;

if((fp=fopen("abc","rt"))==NULL)
{
printf("\nCannot open file strike any key exit!");
exit(1);
}
for (i = 0; fgets(str,1024,fp); i++) {
parseStr(str, i);
}

for (j = 0; j < i; j++) {
printf("%s\t", handinfee[j].sonbr);
printf("%s\t", handinfee[j].msisdn);
printf("%s\t", handinfee[j].staffid);
printf("%d\t", handinfee[j].fee);
printf("%d\n", handinfee[j].flag);
}

fclose(fp);
}
mostideal 2005-06-05
  • 打赏
  • 举报
回复
mark!!!!
yangwuhan 2005-06-05
  • 打赏
  • 举报
回复
以下是取第一个子字符串的例子,其他类推
char* p=str;
char* temp=str;
while('&'!=*temp) ++temp;
*temp++='\0';
strcpy(handinfee.sonbr,p); //strcpy在"string.h"中申明
p=temp;
lemon520 2005-06-05
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct handinfee
{
char sonbr[20+1];
char msisdn[11];
char staffid[10+1];
int fee;
int flag;
}
handinfees[50]={0};

void f(struct handinfee *h,char* str)
{
char *p;
p=strtok(str,"&");
p=strchr(p,'=');
strcpy(h->sonbr,p+1);
p=strtok(NULL,"&");
p=strchr(p,'=');
strcpy(h->msisdn,p+1);
p=strtok(NULL,"&");
p=strchr(p,'=');
strcpy(h->staffid,p+1);
p=strtok(NULL,"&");
p=strchr(p,'=');
h->fee=atoi(p+1);
p=strtok(NULL,"&");
p=strchr(p,'=');
h->flag=atoi(p+1);
}

int main()
{
FILE* fp;
char str[1024]="";
int i=0;
if((fp=fopen("123.txt","rt"))==NULL)
{
printf("\nCannot open file strike any key exit!");
exit(1);
}
while(fgets(str,1024,fp))
{
printf("%s\n",str);
f(&handinfees[i],str);
printf("sonbr=[%s],msisdn=[%s],staffid=[%s],fee=[%d],flag=[%d]\n",
handinfees[i].sonbr,handinfees[i].msisdn,handinfees[i].staffid,handinfees[i].fee,handinfees[i].flag);
i++;
}
fclose(fp);
system("pause");
}
5420 2005-06-05
  • 打赏
  • 举报
回复
关注!

69,373

社区成员

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

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