D
dreamcatcher
I wrote the following program which gets student id, and name, and
score, store them into a file,
use qsort() to sort the score, but seems when I wanted to inquire
specific student's info using id in inq(), i just never get what I
wanted, don't know why. thanx for point out my error.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
// due to this program was orginally written under Visual C++
// in order for C compilers which do not support boolean type
// I have to define such
#define bool int
#define true 1
#define false 0
#define sizeOfArray(a) (sizeof(a)/sizeof(a[0]))
typedef struct tagLink {
int id;
char name[64];
int score;
}Link;
Link lnkArray[4];
void input(void) {
int i;
FILE *fp;
fp=fopen("data.dat","w+");
if(fp==NULL) {
fprintf(stderr,"fopen() in input()\n");
exit(1);
}
for(i=0;i<4;++i) {
puts("<new input");
puts("= ID =");
scanf("%d",&lnkArray.id);
puts("= Name =");
scanf("%s",lnkArray.name);
puts("= Score =");
scanf("%d",&lnkArray.score);
puts("end of input>");
fwrite(&lnkArray,sizeof(Link),1,fp);
}
puts("## before sort ##");
for(i=0;i<4;++i)
printf("%d\t%s\t%d\n",lnkArray.id,lnkArray.name,l-
nkArray.score);
fclose(fp);
}
int cmpD(const void *s1,const void *s2) { // sort in decending order
return ((Link *)s2)->score-((Link *)s1)->score;
}
int cmpA(const void *s1,const void *s2) { // sort in
ascending order
return ((Link *)s1)->score-((Link *)s2)->score;
}
void output(void) {
int i;
FILE *fp;
fp=fopen("data.dat","rb");
if(fp==NULL) {
fprintf(stderr,"fopen() in output()\n");
exit(2);
}
for(i=0;i<4 && !feof(fp);++i)
fread(&lnkArray,sizeof(Link),1,fp);
qsort(lnkArray,sizeOfArray(lnkArray),sizeof(Link),cmpD);
puts("## after sort ##");
for(i=0;i<4;++i)
printf("%d\t%s\t%d\n",lnkArray.id,lnkArray.name,l-
nkArray.score);
fclose(fp);
}
FILE *inq(int id) {
FILE *fp;
Link *lnk;
bool exist=false;
fp=fopen("data.dat","rb");
if(fp==NULL) {
puts("fopen()");
exit(2);
}
while(!feof(fp)) {
lnk=(Link *)malloc(sizeof(Link));
if(lnk==NULL) {
puts("malloc()");
exit(1);
}
fread(lnk,sizeof(Link),1,fp);
if(lnk->id==id) {
exist=true;
break;
}
}
return exist?fp:NULL;
}
void inquire(void) {
int id;
FILE *fp;
Link *lnk;
lnk=(Link *)malloc(sizeof(Link));
if(lnk==NULL) {
puts("malloc()");
exit(3);
}
puts("enter id for inquire:");
scanf("%d",&id);
fp=inq(id);
if(fp==NULL) {
puts("no such record");
exit(4);
}
fread(lnk,sizeof(Link),1,fp);
printf("%d\t%s\t%d\n",lnk->id,lnk->name,lnk->score);
free(lnk);
fclose(fp);
}
int main(void)
{
input();
output();
inquire();
return 0;
}
//// HERE'S THE PROBLEM ////////////
<new input
= ID =
1 // first student id
= Name =
gary // name
= Score =
100 // score
end of input>
<new input
= ID =
2 // second student id
= Name =
benjamin // name
= Score =
65 // score
end of input>
<new input
= ID =
3 // third
= Name =
april
= Score =
78
end of input>
<new input
= ID =
4 // fourth
= Name =
jason
= Score =
89
end of input>
## before sort ##
1 gary 100
2 benjamin 65
3 april 78
4 jason 89
## after sort ##
1 gary 100
4 jason 89
3 april 78
2 benjamin 65
enter id for inquire:
3 // enter April's id, but below ...
4 jason 8 // this is not what I wanted
score, store them into a file,
use qsort() to sort the score, but seems when I wanted to inquire
specific student's info using id in inq(), i just never get what I
wanted, don't know why. thanx for point out my error.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
// due to this program was orginally written under Visual C++
// in order for C compilers which do not support boolean type
// I have to define such
#define bool int
#define true 1
#define false 0
#define sizeOfArray(a) (sizeof(a)/sizeof(a[0]))
typedef struct tagLink {
int id;
char name[64];
int score;
}Link;
Link lnkArray[4];
void input(void) {
int i;
FILE *fp;
fp=fopen("data.dat","w+");
if(fp==NULL) {
fprintf(stderr,"fopen() in input()\n");
exit(1);
}
for(i=0;i<4;++i) {
puts("<new input");
puts("= ID =");
scanf("%d",&lnkArray.id);
puts("= Name =");
scanf("%s",lnkArray.name);
puts("= Score =");
scanf("%d",&lnkArray.score);
puts("end of input>");
fwrite(&lnkArray,sizeof(Link),1,fp);
}
puts("## before sort ##");
for(i=0;i<4;++i)
printf("%d\t%s\t%d\n",lnkArray.id,lnkArray.name,l-
nkArray.score);
fclose(fp);
}
int cmpD(const void *s1,const void *s2) { // sort in decending order
return ((Link *)s2)->score-((Link *)s1)->score;
}
int cmpA(const void *s1,const void *s2) { // sort in
ascending order
return ((Link *)s1)->score-((Link *)s2)->score;
}
void output(void) {
int i;
FILE *fp;
fp=fopen("data.dat","rb");
if(fp==NULL) {
fprintf(stderr,"fopen() in output()\n");
exit(2);
}
for(i=0;i<4 && !feof(fp);++i)
fread(&lnkArray,sizeof(Link),1,fp);
qsort(lnkArray,sizeOfArray(lnkArray),sizeof(Link),cmpD);
puts("## after sort ##");
for(i=0;i<4;++i)
printf("%d\t%s\t%d\n",lnkArray.id,lnkArray.name,l-
nkArray.score);
fclose(fp);
}
FILE *inq(int id) {
FILE *fp;
Link *lnk;
bool exist=false;
fp=fopen("data.dat","rb");
if(fp==NULL) {
puts("fopen()");
exit(2);
}
while(!feof(fp)) {
lnk=(Link *)malloc(sizeof(Link));
if(lnk==NULL) {
puts("malloc()");
exit(1);
}
fread(lnk,sizeof(Link),1,fp);
if(lnk->id==id) {
exist=true;
break;
}
}
return exist?fp:NULL;
}
void inquire(void) {
int id;
FILE *fp;
Link *lnk;
lnk=(Link *)malloc(sizeof(Link));
if(lnk==NULL) {
puts("malloc()");
exit(3);
}
puts("enter id for inquire:");
scanf("%d",&id);
fp=inq(id);
if(fp==NULL) {
puts("no such record");
exit(4);
}
fread(lnk,sizeof(Link),1,fp);
printf("%d\t%s\t%d\n",lnk->id,lnk->name,lnk->score);
free(lnk);
fclose(fp);
}
int main(void)
{
input();
output();
inquire();
return 0;
}
//// HERE'S THE PROBLEM ////////////
<new input
= ID =
1 // first student id
= Name =
gary // name
= Score =
100 // score
end of input>
<new input
= ID =
2 // second student id
= Name =
benjamin // name
= Score =
65 // score
end of input>
<new input
= ID =
3 // third
= Name =
april
= Score =
78
end of input>
<new input
= ID =
4 // fourth
= Name =
jason
= Score =
89
end of input>
## before sort ##
1 gary 100
2 benjamin 65
3 april 78
4 jason 89
## after sort ##
1 gary 100
4 jason 89
3 april 78
2 benjamin 65
enter id for inquire:
3 // enter April's id, but below ...
4 jason 8 // this is not what I wanted