在c语言里,用fprintf写入数据到文件时,为什么会写入一堆错码!
以下是主要代码:voidwrite()//录入数据{system("cls");structClassmanman[1000];inti;intamannum;//将要增...
以下是主要代码:
void write()//录入数据
{
system("cls");
struct Classman man[1000];
int i;
int amannum;//将要增加的通讯个体数
i = 0;
printf("将要录入人次为:\n");
scanf ("%d",&amannum);
printf("请依次录入以下信息\n\n");
printf("姓 名 学 校 电话\n\n");
printf("录入中....\n\n");
i = mannum;
while(i < mannum + amannum)
{
scanf("%s", man[i].name);
scanf("%s", man[i].school);
scanf("%d", &man[i].pnum);
i++;
}
FILE *f;
f = fopen("stucall.txt","a");//a,append 增加 w, write写
for (i = mannum; i < mannum + amannum; i++ );
{
fprintf(f,"%s %s %d\n",man[i].name,man[i].school,man[i].pnum);
}
fclose(f);
mannum += amannum;
}
//初始化
void init()
{
FILE *f;
int i = 0;
if( (f = fopen("stucall.txt", "r")) != NULL)
{
while( !feof(f) )
{
fscanf(f, "%s%s%d\n",man[i].name,man[i].school,&man[i].pnum);
i++;
}
fclose(f);
}
mannum = i;
} 展开
void write()//录入数据
{
system("cls");
struct Classman man[1000];
int i;
int amannum;//将要增加的通讯个体数
i = 0;
printf("将要录入人次为:\n");
scanf ("%d",&amannum);
printf("请依次录入以下信息\n\n");
printf("姓 名 学 校 电话\n\n");
printf("录入中....\n\n");
i = mannum;
while(i < mannum + amannum)
{
scanf("%s", man[i].name);
scanf("%s", man[i].school);
scanf("%d", &man[i].pnum);
i++;
}
FILE *f;
f = fopen("stucall.txt","a");//a,append 增加 w, write写
for (i = mannum; i < mannum + amannum; i++ );
{
fprintf(f,"%s %s %d\n",man[i].name,man[i].school,man[i].pnum);
}
fclose(f);
mannum += amannum;
}
//初始化
void init()
{
FILE *f;
int i = 0;
if( (f = fopen("stucall.txt", "r")) != NULL)
{
while( !feof(f) )
{
fscanf(f, "%s%s%d\n",man[i].name,man[i].school,&man[i].pnum);
i++;
}
fclose(f);
}
mannum = i;
} 展开
2个回答
展开全部
存入时不存入\0,读取时会一直往下读,要注意%s的用法,一般在存数据的时候用fwrite和fread,比较方便
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询