janssenssimon@hotmail.com schrieb:
[moved this part up][color=blue]
> This is my code, but the problem is that when you close and open the
> entire programme it can't printf any of the highscores, or write them
> away for that matter. Non of the 2 functions are completely finished.
> Why does this happen and what can I do about it??[/color]
Please state your problem before giving us your code -- this way
we can read your code with the problem in mind.
If you state your problem, do it systematically:
- What are you trying to do?
- What have you achieved yet?
- Where do you have problems? What are these problems? What would
you have expected?
After reading your description I know that you seem to have
problems with reading to or writing from files. That is not very
much.
[color=blue]
> //de structure om de highscores in op de slagen[/color]
Note that C++/C99 line comments have disadvantages for code
posted to a newsgroup -- if lines wrap around, something different
may happen -- or your code does not compile.
Even if I assume C99, then your code does not compile because at
least
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
is missing.
If you do not give a complete, compiling piece of code illustrating
your problem, then say so. It is, however, possible that the main
problems are in the part you do not show us.
[color=blue]
> typedef struct score{
> char *naam;
> int veld;
> int score;
> struct score *volg;
> }HIGH;
>
>
> void toonhighscores(void)
> {
> int i=6;[/color]
Why 6? Magic numbers are usually a bad idea.
Use a symbolic constant instead.
[color=blue]
> FILE* f;
> HIGH* h;
>
> f=fopen("high.bin","rb");
>
> clrscr();[/color]
Undeclared function.[color=blue]
>
> //als er al highscores zijn,...
> if(f)
> {
> printf("HIGHSCORES\n----------\n\nNaam");
> gotoxy(30,4);
> printf("Veld");
> gotoxy(38,4);
> printf("Score\n\n");
>
> h=malloc(sizeof(HIGH));[/color]
You forgot to check whether h != NULL -- otherwise,
you must not continue.
[color=blue]
> fread(h, sizeof(HIGH), 1, f);
> while(!feof(f))
> {
> printf("%s", h->naam);[/color]
h->naam is a pointer. You cannot store a pointer to a file during
one run of your program and retrieve it in a meaningful way during
another run.
You must store the actual _string_.
[color=blue]
> gotoxy(30,i);[/color]
Unknown function.
[color=blue]
> printf("%.2d", h->veld);
> gotoxy(38,i++);
> printf("%.2d\n", h->score);
>
> fread(h, sizeof(HIGH), 1, f);
> }
> free(h);
> fclose(f);
> }
> //als er nog geen highscores zijn,...
> else
> printf("Nog geen highscores aanwezig in het spel\n");
>
> printf("\nDruk op een toets om terug te keren naar het menu.");
> getch();[/color]
Unknown function.
[color=blue]
> }
>
> //schrijft de gespeelde highscores van dit spel weg
> void schrijfweg_highscores(char *naam, int size, int zetten)
> {
> int geschreven=0;
> FILE *f;
> HIGH *h, *kop=NULL, *staart=NULL, *n;
>
> f=fopen("high.bin", "rb");
>
> if(f)
> {
> h=malloc(sizeof(HIGH));
> fread(h, sizeof(HIGH), 1, f);
> while(!feof(f))
> {
> //alles in lezen in een structure
> //die later terug wordt geschreven in een nieuw bestand
> h->volg=NULL;
> if(kop)
> staart->volg=h;
> else
> kop=h;
> staart=h;
> h=malloc(sizeof(HIGH));
> fread(h, sizeof(HIGH), 1, f);
> }
>
> fclose(f);
> f=fopen("high.bin","wb");
>
> for(h=kop; h; h=h->volg)
> {
> if(!(strcmp(h->naam,naam)) && h->veld==size && h->score>zetten)
> {
> //als de nieuwe data vorige data moet overschrijven
> h->score=zetten;
> geschreven=1;
> }
> fwrite(h, sizeof(HIGH), 1, f);
> }
>
> //als de nieuwe data nog niet in het bestand zit
> if(!geschreven)
> {
> n=malloc(sizeof(HIGH));
> n->naam=naam;
> n->veld=size;
> n->score=zetten;
> n->volg=NULL;
> fwrite(n, sizeof(HIGH), 1, f);
> free(n);
> }
> }
> else
> {
> //als er nog niets in het bestand staat, moeten de scores van het
> gespeelde spel erin komen
> h=malloc(sizeof(HIGH));
> h->naam=naam;
> h->veld=size;
> h->score=zetten;
> h->volg=NULL;
> f=fopen("high.bin", "wb");
> fwrite(h, sizeof(HIGH), 1, f);
> }
>
> //bestand afsluiten
> fclose(f);
>
> //geheugen vrijmaken
> if(kop)
> for(h=kop; h;)
> {
> h=kop->volg;
> free(kop);
> kop=h;
> }
> else
> free(h);
> }[/color]
Consider writing everything into a text file, one line per struct
component. This makes testing etc. much easier.
Then decide whether you want to have arbitrary length strings or
whether you restrict your string length.
In the latter case, change the declaration of "naam" to
char naam[MAX_NAAM_LENGTH+1];
where the "+1" accommodates for the string terminator.
Otherwise, you need a function which can read lines of arbitrary length
and allocates sufficient memory for the string read.
Only with fixed length strings, you can read in the entries from
a binary file blockwise.
However, if you had printed everything as text to the file, you
would either have found your conceptual error earlier or would not
have introduced in the first place.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.