Thread: FILES (read & write)

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    FILES (read & write)

    I am traing to read and then to write.
    I don't know why but I have to write before reading.

    It doesn't work this way:
    Code:
    FILE *in, *out;
    int i, max;
        in = fopen ("x.txt","rt");
        if (in == NULL)
           return 0;
        for (max = fgetc(in);!feof(in);max += fgetc(in) * i)
            i *= 10;
        fclose(in);
    HELP plz \:

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "rt" should be "r". You should note that unless the file x.txt exists, then a call to fopen with trying to open a file for reading using "r" will fail.
    Make sure the file exists first, then you can read first and write later.
    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.

  3. #3
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    humm I create the file manually before running the program..
    Anyway how can I read the number from the file?
    For example: 100
    And why it's "r" and not "rt"?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Assuming we can expect the file to exist, you should use "r+" (the "t" in the mode would be a Microsoft extension, and it's default to be text anyways, so for compatibility, don't use it).

    "r+" means that the file is read and write.

    If you don't know if the file exists, but you still want to read from it (rather than just add to the end of it), you can use "a+". This sets the file-position to the end of the file, so you it needs to be combined with fseek() or rewind() to get to a different place than end-of-file.

    --
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by gavra View Post
    humm I create the file manually before running the program..
    Well, then it's likely you specified incorrect path. Try an absolute path and see if that works.

    Anyway how can I read the number from the file?
    For example: 100
    I assume you just wrote 100 into notepad and saved the file?
    If so, then use fgets to read line and then strtod to convert into a integer (to store in an integer variable).
    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.

  6. #6
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Ok, but if I want to create a file and the to read and maybe to write to what should I use?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Well, then it's likely you specified incorrect path. Try an absolute path and see if that works.


    I assume you just wrote 100 into notepad and saved the file?
    If so, then use fgets to read line and then strtod to convert into a integer (to store in an integer variable).
    strtol is better than strtod if you want to convert integer values. strtod stands for "String to Double", strtol is "String To Long Integer".

    --
    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.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    Ok, but if I want to create a file and the to read and maybe to write to what should I use?
    "a+" combined with fseek() or rewind() [rewind(file) is the same as fseek(file, 0, SEEK_SET) - but I think some runtime libraries do not have rewind().]

    --
    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.

  9. #9
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Quote Originally Posted by Elysia View Post
    Well, then it's likely you specified incorrect path. Try an absolute path and see if that works.


    I assume you just wrote 100 into notepad and saved the file?
    If so, then use fgets to read line and then strtod to convert into a integer (to store in an integer variable).
    Ho that's waht I need.. to convert into integer.
    Can you explain me about "strtod" plz?

  10. #10
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Or you just explain me about the "strtod"..
    And how do I check if any file is exist?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    strtol is better than strtod if you want to convert integer values. strtod stands for "String to Double", strtol is "String To Long Integer".
    Yes, of course, I tend to mix those two.

    Quote Originally Posted by gavra View Post
    Ho that's waht I need.. to convert into integer.
    Can you explain me about "strtod" plz?
    Code:
    char mystr[] = "100";
    long n = strtol(mystr, NULL, 10);
    To check if a file exist, open file with fopen. If fopen returns NULL, then it doesn't exist or you don't have permission to access that file.
    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.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    Or you just explain me about the "strtod"..
    And how do I check if any file is exist?
    If you want to know if the file exists, try opening for reading first, if that's not working, then open for writing instead (check status of this as well, could be that you can't open/create files for some other reason, e.g. you haven't got sufficient rights, the disk is a CDROM, so you can't create files, etc, etc).

    Alternativelys, if you use "a+", and use ftell() without fseek or rewind, if you get zero from ftell, then the file was just created (or at least, the file is empty - it could of course be an existing empty file, but for most intents and purposes, that's the same thing as no file).


    strtol is what you want, since "max" is an integer, not a double.
    http://www.hmug.org/man/3/strtol.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.

  13. #13
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    thx.
    Problem..
    Code:
        FILE *in, *out;
        char str[15];
        int max;
        in = fopen ("x.txt","t");
        if (in == NULL)
        {
           out = fopen("x.txt","w");
           fclose (out);
        }
        fgets (str,15,in);
        max = strtol (str,NULL,15);
        printf("%d",max);
        fclose(in);
    what now?

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That looks about right, so what do you want it to do that it's not currently doing?

    Edit: What I'm trying to say is that you haven't explained what your overall goal is with your program. So it's not very easy for us to say what you should or shouldn't do within your code...

    --
    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.

  15. #15
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    It gives me a "report".
    I'll say what I am traing to do so we all understand each other better.
    I wrote a game today (feed the snake) and I want to save records.
    First of all I want to check if the score is bigger then the last record.
    If so I want to change the record (and maybe add a name too).

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, 11:25 AM
  3. How to read files?
    By Raigne in forum C++ Programming
    Replies: 17
    Last Post: 11-23-2005, 05: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, 09:41 PM