c语言读取txt文件中数字、字符串问题

编了个两个程序分别写入和读取数据,可是写入后杜不出来,只能读第一行,高手指点下!以下是代码关键部分:===================================... 编了个两个程序分别写入和读取数据,可是写入后杜不出来,只能读第一行,高手指点下!
以下是代码关键部分:
=================================================================
写文件:
int save(char *fname)
{
FILE *fp;
int i;
if ((fp = fopen(fname,"w")) == NULL)
{
printf("\n>>打开文件 %s 失败!\n>>数据更新失败!\n",fname);
return(0);
}
fprintf(fp,"@id_now=%d|count=%d|rate=%.2f\n",id_now,count,rate);
for (i=0;i<count;i++)
{
if (fprintf(fp,">>%d %s %.2f %s %d %d %d\n",user[i].id,user[i].name,user[i].money,user[i].md5code,user[i].state,user[i].power,user[i].bank) < 0)
{
printf("\n写入卡号:%d账户信息到 %s 文件失败!\n>>数据更新失败!\n",user[i].id,fname);
return(0);
}
}
fclose(fp);
printf("\n>>数据保存成功!\n");
return(1);
}

==================================================
写入数据后文件内容如下:
@id_now=1001003|count=2|rate=0.20
>>10010001 liuyang 1000.01 e10adc3949ba59abbe56e057f20f883e 1 1 1
>>10010002 chenlin 1111.11 e78b5e0434b33bbbd363d0748a8bbe5a 1 1 1

=================================================================
读文件代码:
void load(char *fname)
{
FILE *fp;
int i;

if ((fp = fopen(fname,"r")) == NULL)
{
printf("\n>>更新文件 %s 失败!\n>>数据更新失败!\n",fname);
system("pause");
exit(0);
}
i=0;
fscanf(fp,"@id_now=%d|count=%d|rate=%f",&id_now,&count,&rate);
printf(">>id_now=%ld|count=%d|rate=%.2f\n",id_now,count,rate);
while(!feof(fp)&&i<count)
{
fscanf(fp,">>%d %s %f %s %d %d %d",&user[i].id,&user[i].name,&user[i].money,&user[i].md5code,&user[i].state,&user[i].power,&user[i].bank);
printf(">>%d|%s|%.2f|%s|%d|%d|%d\n",user[i].id,user[i].name,user[i].money,user[i].md5code,user[i].state,user[i].power,user[i].bank);
i++;
//fseek(fp,2l,SEEK_CUR);
}
fclose(fp);
count = i;

}

=================================================================
使用的部分全局变量:
#define SIZE 10 //限定最大数

struct clients
{
long id;
char pwd[7];
char name[20];
float money;
char md5code[33];
int state;
int power;
int bank;
}user[SIZE] =
{
{10010001,"123456","liuyang",1000.01,"e10adc3949ba59abbe56e057f20f883e",1,1,1},
{10010002,"100806","chenlin",1111.11,"e78b5e0434b33bbbd363d0748a8bbe5a",1,1,1}
};
long id_now=1001003;
int count=2;
float rate=0.2;

其中写文件时需要用到的变量都赋值了,而读文件的代码编译时给的是没赋值的,经测试问题不是在这。应该是读文件的地方
展开
 我来答
匿名用户
推荐于2016-09-28
展开全部
fscanf(fp,"@id_now=%d|count=%d|rate=%f",&id_now,&count,&rate);

改为

fscanf(fp,"@id_now=%d|count=%d|rate=%f\n",&id_now,&count,&rate);

fscanf(fp,">>%d %s %f %s %d %d %d",&user[i].id,&user[i].name,&user[i].money,&user[i].md5code,&user[i].state,&user[i].power,&user[i].bank);

改为:

fscanf(fp,">>%d %s %f %s %d %d %d\n",&user[i].id,&user[i].name,&user[i].money,&user[i].md5code,&user[i].state,&user[i].power,&user[i].bank);
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式