首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

,可能是链表指针或是使用文件函数不当,为什么写入的和读出的总是不一样

2012-03-04 
在线等,可能是链表指针或是使用文件函数不当,为什么写入的和读出的总是不一样?我觉得主要的问题在add函数

在线等,可能是链表指针或是使用文件函数不当,为什么写入的和读出的总是不一样?
我觉得主要的问题在add函数里面...
#include <stdio.h>
#include <malloc.h>

struct   student{
unsigned   ID;
char   name[8];
char   sex[4];
char   tel[15];
unsigned   addr;
struct   student   *next;
};
struct   student   *head,*p1,*p2;
void   add()
{
printf( "请您添加记录!\n ");
printf( "学号       姓名       性别       电话号码       宿舍号\n ");

int   n=0;
p1=p2=(struct   student*)malloc(sizeof(   struct   student));
scanf( "%d%s%s%s%d ",&p1-> ID,&p1-> name,&p1-> sex,&p1-> tel,&p1-> addr);
head=NULL;
FILE   *fp;
if((fp=fopen( "stutable.txt ", "ab "))==NULL)
{
printf( "open   file   error,cannt   input!\n ");
return   ;
}
while(p1-> ID   !=0)
{      
if(fwrite(&p1,sizeof(   struct   student),1,fp)!=1)
{printf( "file   write   error\n ");exit   0;   }
printf( "请输入下一条记录:\n ");
n=n+1;
if(n==1)head=p1;
else   p2-> next   =p1;
p2=p1;
p1=(struct   student*)malloc(sizeof(   struct   student));
scanf( "%d%s%s%s%d ",&p1-> ID,&p1-> name,&p1-> sex,&p1-> tel,&p1-> addr);
}
fclose(fp);
        p2-> next=NULL;

}
int   del()
{
printf( "del\n ");
return   0;
}
int   find()
{
printf( "find\n ");
return   0;
}
int   update()
{
printf( "update\n ");
return   0;
}
int   list()
{
        printf( "将列出所有的记录\n ");
FILE   *fp;
fp=fopen( "stutable.txt ", "wb ");
rewind(fp);
while(!feof(fp))
{
fread(&p2,sizeof(   struct   student),1,fp);
        printf( "%d   %s   %s   %s   %d\n ",p2-> ID   ,p2-> name,p2-> sex,p2-> tel,p2-> addr);
}
fclose(fp);
return   0;
}
int   main()
{
ex:printf( "欢迎使用简单学生管理系统\n ");
printf( "\t1.添加记录\n ");
printf( "\t2.删除记录\n ");
printf( "\t3.查找记录\n ");
printf( "\t4.修改记录\n ");
printf( "\t5.列出全部记录\n ");
printf( "\t6.退出系统\n ");

for(int   i=0;i <=9;i++)
{
printf( "\n请选择您的操作:   ");
int   intnum;
scanf( "%d ",&intnum);
if(intnum==1)   {   add();   goto   ex;}
if(intnum==2)   {   del();   goto   ex;}
if(intnum==3)   {   find();goto   ex;}
if(intnum==4)   {   update();goto   ex;}
if(intnum==5)   {   list();goto   ex;}
if(intnum==6)   {printf( "系统退出,再见\n ");   return   0;}
else     printf( "您输入错误,请重新输入! ");
}
return   0;
}

[解决办法]
int list()
{
printf( "将列出所有的记录\n ");
FILE *fp;
fp=fopen( "stutable.txt ", "wb "); > > fp=fopen( "stutable.txt ", "rb ");读文件
rewind(fp);
while(!feof(fp))


{
fread(&p2,sizeof( struct student),1,fp);
printf( "%d %s %s %s %d\n ",p2-> ID ,p2-> name,p2-> sex,p2-> tel,p2-> addr);
}
fclose(fp);
return 0;
}
[解决办法]
//以下程序没有释放内存, LZ自己做把. (1)-(5)是改正的地方.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct student{
unsigned ID;
char name[8];
char sex[4];
char tel[15];
unsigned addr;
struct student *next;
};
struct student *head,*p1,*p2;
void add()
{
printf( "请您添加记录!\n ");
printf( "学号 姓名 性别 电话号码 宿舍号\n ");

int n=0;
p1=p2=(struct student*)malloc(sizeof( struct student));

///(1)
scanf( "%d%s%s%s%d ",&p1-> ID,p1-> name,p1-> sex,p1-> tel,&p1-> addr);
head=NULL;
FILE *fp;
if((fp=fopen( "stutable.txt ", "ab "))==NULL)
{
printf( "open file error,cannt input!\n ");
return ;
}
while(p1-> ID !=0)
{
if(fwrite(p1,sizeof( struct student),1,fp)!=1)
{printf( "file write error\n ");exit(0); }
printf( "请输入下一条记录:\n ");
n=n+1;
if(n==1)head=p1;
else p2-> next =p1;
p2=p1;
p1=(struct student*)malloc(sizeof( struct student));

////(2)
scanf( "%d%s%s%s%d ",&p1-> ID,p1-> name,p1-> sex,p1-> tel,&p1-> addr);
}
fclose(fp);
p2-> next=NULL;

}
int del()
{
printf( "del\n ");
return 0;
}
int find()
{
printf( "find\n ");
return 0;
}
int update()
{
printf( "update\n ");
return 0;
}
int list()
{
///(3)
if (!p2) p2=(struct student*)malloc(sizeof( struct student));

printf( "将列出所有的记录\n ");
FILE *fp;

///(4)
fp=fopen( "stutable.txt ", "rb ");
while(!feof(fp))
{
if(fread(p2,sizeof( struct student),1,fp))
printf( "%d %s %s %s %d\n ",p2-> ID ,p2-> name,p2-> sex,p2-> tel,p2-> addr);
}
fclose(fp);
return 0;
}
int main()
{
///(5)
p1 = p2 = head = 0;

ex:printf( "欢迎使用简单学生管理系统\n ");
printf( "\t1.添加记录\n ");
printf( "\t2.删除记录\n ");
printf( "\t3.查找记录\n ");
printf( "\t4.修改记录\n ");
printf( "\t5.列出全部记录\n ");
printf( "\t6.退出系统\n ");

for(int i=0;i <=9;i++)
{
printf( "\n请选择您的操作: ");
int intnum;
scanf( "%d ",&intnum);
if(intnum==1) { add(); goto ex;}
if(intnum==2) { del(); goto ex;}
if(intnum==3) { find();goto ex;}
if(intnum==4) { update();goto ex;}
if(intnum==5) { list();goto ex;}
if(intnum==6) {printf( "系统退出,再见\n "); return 0;}
else printf( "您输入错误,请重新输入! ");
}
return 0;
}

[解决办法]
while(!feof(fp))
{
if(fread(p2,sizeof( struct student),1,fp))
printf( "%d %s %s %s %d\n ",p2-> ID ,p2-> name,p2-> sex,p2-> tel,p2-> addr);
}
_________________________________________________________________________________
这个地方会多打印一条做后的记录吧....

改成下面看下:
while ( fread(&p2,sizeof( struct student),1,fp) == 1 )


printf( "%d %s %s %s %d\n ",p2-> ID ,p2-> name,p2-> sex,p2-> tel,p2-> addr);
[解决办法]
scanf( "%d%s%s%s%d ",&p1-> ID,&p1-> name,&p1-> sex,&p1-> tel,&p1-> addr);

字符串输入时用不 如:&p1-> name 就不用加地址符&了,

还有就是输入的格式:
scanf( "%d %s %s %s %d ",&p1-> ID,&p1-> name,&p1-> sex,&p1-> tel,&p1-> addr);记得格式符之间最好加上空格

热点排行