I have noticed that even though the file exist fopen returns NULL \:
I have noticed that even though the file exist fopen returns NULL \:
So, what you are doing is a high-score list, yes?
If that's the case, then you need to essentially implement an "insertion sort". Inserting into a file is not possible, so if you want a limited high-score list (e.g. 10, 50 or 100 highest scores), then you should probably read all the current values in the file into an array [with 10, 50 or 100 elements, of course], then insert the new element and write all the data back to the file (after using fseek or rewind to get back to the beginning of the file).
Bear in mind that you will need to keep track of the number of entries, and discard the lowest one when the table is full.
If you want to keep all scores from today and forever after, then you will need a different approach, either store all results in the order they came, and then use a sorting or selection mechanism to find the entries you want. Or read from the original file, write to a new file (adding the new entry at the appropriate place, just before the entry that is smaller (if you want equal scores "newest first" then compare less or equal instead of less than)).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Are you sure you are opening the file in the same directoy as you think it is? use getcwd() to find what your application is actually using.
http://www.hmug.org/man/3/getcwd.php
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
"t" is not a valid mode. It's either "r", "w" or "a", and a "+" can be added to form "r+" and "w+".
Second, read your logic there. If you fail to open a file, you must not try to read from it.
Trying to close an unopened file will also crash the program.
Read the documentation for strtol again. The last argument in the base.
thank you.
Yep that's what I am traing to do.
It's not that easy as I thought.
Same directory, I think.. and the function you give works like this: getcwd(*fp,size of what?) ?!
It works only if the file is exist.
I want it to create the file if it doesn't exist..Code:int read_write (int score) { FILE *in, *out; char str[15]; int max; in = fopen ("Highscore.txt","r+"); if (in == NULL) { out = fopen("Highscore.txt","w"); fputc ('0',out); fclose (out); in = fopen ("Highscore.txt","r+"); } fgets (str,15,in); max = atoi(str); if (score > max) { printf ("\nNew Highscore!\n"); rewind (in); fprintf (in,"%d",score); } fclose(in); }
Thank again.
Again, this code is bad. Let me give you a scenario:
Try to open Highscore.txt. File does not exist. Fail.
Try to open Highscore.txt for writing. Insufficient permissions. Fail.
Write '0' to out. File is not open. Crash.
Close file which is not open. Fail. Crash.
Open Highscore.txt for reading. File does not exist. Fail.
Try to read a line from the Highscore.txt file. File is not open. Fail. Crash.
Atoi is still non-standard. Again, use strtol.
Score > max condition is true.
Print text "\nNew Highscore!\n"
Rewind input file to beginning. File is not open. Fail. Crash.
Print new highscore.
Close input file. File is not open. Crash.
To open a file for writing use "w", as you do. You may even open is at "w+" to open the file for writing (creating it if it doesn't exist) and for reading.
Thank's.
"Atoi is still non-standard. Again, use strtol."
I tried but it's complicated (so I use the simplest) I didn't understand the need of the 3 arguments and what means each of them.