[#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
char name[20];
long long int numb;
struct Student * pNext;
} ;
struct Student_1
{
char name[20];
long long int numb;
} ;
int n;
struct Student * create()
{
FILE * fp;
if((fp=fopen("student","wb"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
struct Student *head=NULL,*pEnd,*pNew,*p;
n=0;
char ch;
pEnd=pNew=(struct Student*)malloc(sizeof(struct Student));
do
{
printf("请输入学生姓名:\n");
scanf("%s",pNew->name);
getchar();
printf("请输入学生学号:\n");
scanf("%lld",&pNew->numb );
getchar();
n++;
if(n==1)
{
pNew->pNext=head;
head=pNew;
}
else
{
pNew->pNext=NULL;
pEnd->pNext=pNew;
pEnd=pNew;
}
printf("是否继续录入(y/n):\n");
ch=getchar();
getchar();
if(ch=='y')
pNew=(struct Student*)malloc(sizeof(struct Student));
}while(ch=='y');
p=head;
while(p!=NULL)
{
if(fwrite(p,sizeof(struct Student_1),1,fp)!=1)
{
printf("录入出错!\n");
}
p=p->pNext;
}
fclose(fp);
return head;
}
void print(struct Student* head)
{
struct Student *p;
p=head;
printf("姓名\t\t学号\n");
while(p!=NULL)
{
printf("%s\t\t%lld\n",p->name,p->numb);
p=p->pNext;
}
}
int main()
{
struct Student* head;
head=create();
system("cls");
print(head);
return 0;
}
这是用c写的用链表形式将数据以二进制形式写入文件;#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
char name[20];
long long int numb;
struct Student * pNext;
} ;
struct Student_1
{
char name[20];
long long int numb;
} ;
int n;
struct Student * fprint()
{
FILE *fp;
struct Student *head=NULL,*pnew,*pend;
if((fp=fopen("student","rb"))==NULL)
{
printf("不能打开文件!");
exit(0);
}
n=0;
while(!feof(fp))
{
n++;
pnew=(struct Student*)malloc(sizeof(struct Student));
fread(pnew,sizeof(struct Student_1),1,fp);
if(n==1)
{
pnew->pNext=head;
head=pnew;
pend=pnew;
}
else
{
pend->pNext=pnew;
pend=pnew;
pnew->pNext=NULL;
}
}
return head;
}
void print(struct Student* head)
{
struct Student *p;
p=head;
printf("姓名\t\t学号\n");
while(p!=NULL)
{
printf("%s\t\t%lld\n",p->name,p->numb);
p=p->pNext;
}
}
int main()
{
struct Student* head;
head=fprint();
print(head);
return 0;
}
这是对应上面的,可以将数据从文件中读取,但是问题是我录入一组数据,输出两组数据(第二组是乱码),每一次输出都比输入数据多一组?这问题出在哪里了?附上图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
char name[20];
long long int numb;
struct Student *pNext;
};
struct Student_1
{
char name[20];
long long int numb;
};
int n;
struct Student *fprint()
{
FILE *fp;
struct Student *head = NULL, *pnew, *pend;
if((fp = fopen("student", "rb")) == NULL)
{
printf("不能打开文件!");
exit(0);
}
n = 0;
/*while(!feof(fp))
{
n++;
pnew = (struct Student*)malloc(sizeof(struct Student));
fread(pnew, sizeof(struct Student_1), 1, fp);
if(n == 1)
{
pnew->pNext = head;
head = pnew;
pend = pnew;
}
else
{
pend->pNext = pnew;
pend = pnew;
pnew->pNext = NULL;
}
}*/
while(1)
{
n++;
pnew = (struct Student*)malloc(sizeof(struct Student));
fread(pnew, sizeof(struct Student_1), 1, fp);
if(feof(fp))
{
free(pnew);
break;
}
if(n == 1)
{
pnew->pNext = head;
head = pnew;
pend = pnew;
}
else
{
pend->pNext = pnew;
pend = pnew;
pnew->pNext = NULL;
}
}
return head;
}
void print(struct Student* head)
{
struct Student *p;
p = head;
printf("姓名\t\t学号\n");
while(p != NULL)
{
printf("%s\t\t%lld\n", p->name, p->numb);
p = p->pNext;
}
}
int main(void)
{
struct Student* head;
head = fprint();
print(head);
return 0;
}
|