FILES (read & write)

This is a discussion on FILES (read & write) within the C Programming forums, part of the General Programming Boards category; I have noticed that even though the file exist fopen returns NULL \:...

  1. #16
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    I have noticed that even though the file exist fopen returns NULL \:

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    I have noticed that even though the file exist fopen returns NULL \:
    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.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    "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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    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?) ?!

  6. #21
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Quote Originally Posted by Elysia View Post
    "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.
    You right, it's just I havent play so many time with those things so I don't know them so well..
    LOL I'am so fool XD
    Last edited by gavra; 06-12-2008 at 04:24 PM.

  7. #22
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    It works only if the file is 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);
    }
    I want it to create the file if it doesn't exist..
    Thank again.

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    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.

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Quote Originally Posted by gavra View Post
    "Atoi is still non-standard. Again, use strtol."
    If you only need the functionality of atoi when using strtol, you can do:
    strtol(number, NULL, 10);
    You'll learn what the extra arguments are when you gain experience.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read write lock in C#
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-16-2008, 08:49 AM
  2. Replies: 5
    Last Post: 03-18-2006, 10:25 AM
  3. How to read files?
    By Raigne in forum C++ Programming
    Replies: 17
    Last Post: 11-23-2005, 04:03 PM
  4. Replies: 1
    Last Post: 07-24-2002, 06:33 AM
  5. how to read and write files in c++
    By kerick in forum C++ Programming
    Replies: 6
    Last Post: 03-29-2002, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21