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",&lnk Array.id);
puts("= Name =");
scanf("%s",lnkA rray.name);
puts("= Score =");
scanf("%d",&lnk Array.score);
puts("end of input>");
fwrite(&lnkArra y,sizeof(Link), 1,fp);
}
puts("## before sort ##");
for(i=0;i<4;++i )
printf("%d\t%s\ t%d\n",lnkArray .id,lnkArray.na me,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(lnk Array),sizeof(L ink),cmpD);
puts("## after sort ##");
for(i=0;i<4;++i )
printf("%d\t%s\ t%d\n",lnkArray .id,lnkArray.na me,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,sizeo f(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,sizeo f(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 4 1908
On Mon, 15 Sep 2003 09:57:25 -0400, dreamcatcher
<me*********@db forums.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",&lnk Array.id);
scanf("%d",&lnk Array[i].id);
puts("= Name ="); scanf("%s",lnkA rray.name);
scanf("%s",lnkA rray[i].name);
puts("= Score ="); scanf("%d",&lnk Array.score);
scanf("%d",&lnk Array[i].score);
puts("end of input>");
fwrite(&lnkArra y,sizeof(Link), 1,fp);
fwrite(&lnkArra y[i],sizeof(Link),1 ,fp); }
puts("## before sort ##"); for(i=0;i<4;++i ) printf("%d\t%s\ t%d\n",lnkArray .id,lnkArray.na me,lnkArray.sco re);
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,sizeo f(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.
In 'comp.lang.c', Nick Austin <ni**********@n ildram.co.uk> wrote: Better, but this code still doesn't compile! I had to modify input() as follows: scanf("%d",&lnk Array.id);
scanf("%d",&lnk Array[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**********@no os.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/
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
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_FAILU RE);
}
fp_write = fopen(fn_write, mode_write);
if (fp_write == NULL) {
fputs("fopen() in input()", stderr);
exit(EXIT_FAILU RE);
}
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_FAILU RE);
}
fp_read = fopen(fn_read, mode_read);
if (fp_read == NULL) {
fputs("fopen() in output()\n", stderr);
exit(EXIT_FAILU RE);
}
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_FAILU RE);
}
fp_read = fopen(fn_read, mode_read);
if (fp_read == NULL) {
fputs("inq() fopen() failure\n", stderr);
exit(EXIT_FAILU RE);
}
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 comments on
past experience, research articles, comments on the matter would be
much appreciated. I suspect something like C would be the best based
on...
|
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 difficult to know what
is going on.
One of these Order Forms you can see here...
http://www.cardman.co.uk/orderform.php3
|
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 PowerC, a
16-bit DOS compiler); if there are nonstandard or "accidentally-works"
aspects, please let me know.
{This is the sort of situation where...
|
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) like it because you can
split the code from the design. That is logical. But if you are the only
one working on the code, it seem a little...
|
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.
| |
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 solution.
My code can be downloaded from here:
http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some
scripts for GNU/Linux system...
|
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
|
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 users request are a part of the OpenSource
community. Many if not most of those applications strongly require the
presence of the GNU compiling...
|
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, including the BCL, ASP.NET and LINQ) available both for
viewing and debugging into.
I won't go into all the details here, as they're covered on Scott...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |