473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why the code doesn't work the way I wanted ?


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
--
Posted via http://dbforums.com
Nov 13 '05 #1
4 1899
On Mon, 15 Sep 2003 09:57:25 -0400, dreamcatcher
<me*********@dbforums.com> wrote:

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.
Better, but this code still doesn't compile! I had to modify
input() as follows:
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);
scanf("%d",&lnkArray[i].id);
puts("= Name =");
scanf("%s",lnkArray.name);
scanf("%s",lnkArray[i].name);
puts("= Score =");
scanf("%d",&lnkArray.score);
scanf("%d",&lnkArray[i].score);
puts("end of input>");

fwrite(&lnkArray,sizeof(Link),1,fp);
fwrite(&lnkArray[i],sizeof(Link),1,fp);

}

puts("## before sort ##");
for(i=0;i<4;++i)
printf("%d\t%s\t%d\n",lnkArray.id,lnkArray.name,ln kArray.score);

fclose(fp);
}
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);
}
The problem here is that inq() has already read the record that
matches the id you wanted.
fread(lnk,sizeof(Link),1,fp);
This reads the record after the matching record.

printf("%d\t%s\t%d\n",lnk->id,lnk->name,lnk->score);

free(lnk);
fclose(fp);
}


Nick.

Nov 13 '05 #2
In 'comp.lang.c', Nick Austin <ni**********@nildram.co.uk> wrote:
Better, but this code still doesn't compile! I had to modify
input() as follows:

scanf("%d",&lnkArray.id);


scanf("%d",&lnkArray[i].id);


This guy must be a troll, because we spend our time to correct his code, and
he continues to post the same errors...

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #3

i'm learning the language, so don't be so mad at a rookie. thanx for the
critics anyway. and I still don't know how to modify the code to get the
correct output.
--
Posted via http://dbforums.com
Nov 13 '05 #4
dreamcatcher wrote:
i'm learning the language, so don't be so mad at a rookie.
thanx for the critics anyway. and I still don't know how
to modify the code to get the correct output.
/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

#define LINKS 4

struct tagLink {
unsigned id;
char name[64];
unsigned score;
};

struct statusLink {
struct tagLink tag;
int status;
};

void input(char *, char *, size_t);
void output(char *, char *, size_t);
int cmpD(const void *, const void *);
struct statusLink inq(unsigned, char *, char *, size_t);
void inquire(char *, char *, size_t);

int main(void)
{
static char fn_write[] = "data.dat";
char *fn_read = fn_write;
char *mode_write = "wb", *mode_read = "rb";
size_t links = LINKS;

input(fn_write, mode_write, links);
output(fn_read, mode_read, links);
inquire(fn_read, mode_read, links);
return 0;
}

int cmpD(const void *s1,const void *s2)
{
return ((struct tagLink *)s2) -> score ((struct tagLink *)s1) -> score ? -1 : ((struct tagLink *)s1) -> score ((struct tagLink *)s2) -> score ;

}

void input(char *fn_write, char *mode_write, size_t links)
{
size_t i;
FILE *fp_write;
struct tagLink *lnkArray;

lnkArray = malloc(links * sizeof *lnkArray);
if (!lnkArray) {
fputs("malloc() in input()", stderr);
exit(EXIT_FAILURE);
}
fp_write = fopen(fn_write, mode_write);
if (fp_write == NULL) {
fputs("fopen() in input()", stderr);
exit(EXIT_FAILURE);
}
for (i = 0; links != i; ++i) {
puts("<new input");
puts("= ID =");
scanf("%u", &lnkArray[i].id);
puts("= Name =");
scanf("%s", lnkArray[i].name);
puts("= Score =");
scanf("%u", &lnkArray[i].score);
puts("end of input>");
fwrite(lnkArray + i, sizeof *lnkArray, 1, fp_write);
}
fclose(fp_write);
puts("## before sort ##");
for (i = 0; links != i; ++i) {
printf("%u %s %u\n",
lnkArray[i].id, lnkArray[i].name, lnkArray[i].score);
}
free(lnkArray);
}

void output(char *fn_read, char *mode_read, size_t links)
{
size_t i;
FILE *fp_read;
struct tagLink *lnkArray;

lnkArray = malloc(links * sizeof *lnkArray);
if (!lnkArray) {
fputs("malloc() in output()", stderr);
exit(EXIT_FAILURE);
}
fp_read = fopen(fn_read, mode_read);
if (fp_read == NULL) {
fputs("fopen() in output()\n", stderr);
exit(EXIT_FAILURE);
}
fread(lnkArray, links * sizeof *lnkArray, 1, fp_read);
fclose(fp_read);
qsort(lnkArray, links, sizeof *lnkArray, cmpD);
puts("## after sort ##");
for (i = 0; links != i; ++i) {
printf("%u %s %u\n",
lnkArray[i].id, lnkArray[i].name, lnkArray[i].score);
}
free(lnkArray);
}

struct statusLink
inq(unsigned id, char *fn_read, char *mode_read, size_t links)
{
unsigned i;
struct statusLink lnk;
FILE *fp_read;
struct tagLink *lnkArray;

lnkArray = malloc(links * sizeof *lnkArray);
if (!lnkArray) {
fputs("malloc() in inq()", stderr);
exit(EXIT_FAILURE);
}
fp_read = fopen(fn_read, mode_read);
if (fp_read == NULL) {
fputs("inq() fopen() failure\n", stderr);
exit(EXIT_FAILURE);
}
fread(lnkArray, links * sizeof *lnkArray, 1, fp_read);
fclose(fp_read);
lnk.status = 0;
for (i = 0; links != i; ++i) {
if (lnkArray[i].id == id) {
lnk.tag = lnkArray[i];
lnk.status = 1;
break;
}
}
free(lnkArray);
return lnk;
}

void inquire(char *fn_read, char *mode_read, size_t links)
{
unsigned id;
struct statusLink slnk;
struct tagLink lnk;

puts("enter id for inquire:");
scanf("%u", &id);
slnk = inq(id, fn_read, mode_read, links);
if (!slnk.status) {
puts("no such record");
} else {
lnk = slnk.tag;
printf("%u %s %u\n",
lnk.id, lnk.name, lnk.score);
}
}

/* END new.c */

--
pete
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
39
by: gtippery | last post by:
Newbie-ish questions - I've been away from C for a _long_ time. It seems to me that there ought to be easier (or at least shorter) ways to do what this does. It does compile & run for me (with...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
28
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
239
by: Eigenvector | last post by:
My question is more generic, but it involves what I consider ANSI standard C and portability. I happen to be a system admin for multiple platforms and as such a lot of the applications that my...
66
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.