#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0

    File read Problem


    Hi,
    The following code with fgetc() and fprintf() does everything wrong. fgetc() always seems to get wrong characters, and fprintf() doesn't do anything at all. I just can't find out what I am doing wrong with this code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main() {
      
        FILE* f1 = fopen("C:\c.txt", "r");
    
        int c = fgetc(f1);
        printf("%c", c);
    
    
        fprintf(f1, "Test:\n");
    
        fclose(f1);
    }
    Where is the problem?
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    You need to declare c as type char, not int.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by Schol-R-LEA
    You need to declare c as type char, not int.
    I've tried, but I still get the same result. AFAIK, fgetsc returns an integer anyway, which is then converted to char. And normally printf should then convert the integer back to char, based on ASCII values, not?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Your problem is possibly contained in this line:
    Code:
    FILE* f1 = fopen("C:\c.txt", "r");
    You really should check to insure your file opened correctly.

    And don't forget that the '\' character is considered the escape character so you would need two of these characters in that string. Also using the "root" directory is usually considered bad, I recommend you remove the "C:\" completely and let the program use the working directory instead,.

    Jim
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by jimblumberg
    Your problem is possibly contained in this line:
    Code:
    FILE* f1 = fopen("C:\c.txt", "r");
    You really should check to insure your file opened correctly.

    And don't forget that the '\' character is considered the escape character so you would need two of these characters in that string.
    That was the mistake.. I even read about that somewhere, but I've totally forgotten about it :chomp:
    Thanks!

    Originally Posted by jimblumberg
    Also using the "root" directory is usually considered bad, I recommend you remove the "C:\" completely and let the program use the working directory instead,.
    What do you mean? A path that doesn't start with "C:\" ? What would that be in Windows 7?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    What do you mean? A path that doesn't start with "C:\" ? What would that be in Windows 7?
    Something like:
    Code:
    FILE* f1 = fopen("c.txt", "r");
    Jim
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by jimblumberg
    Something like:
    Code:
    FILE* f1 = fopen("c.txt", "r");
    Jim
    Ah, kay. So something like the relative path to where my program is? Or some path that is noted in the PATH environment ehrm, thing?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    S
    o something like the relative path to where my program is?
    That is not a relative path. A relative path is something like:
    Code:
    "someDirectory/yourFile"
    The example I gave is using the current working directory.

    Or some path that is noted in the PATH environment ehrm, thing?
    C/C++ programs don't search the PATH they must be told exactly where the file exists in your directory structure.

    I really suggest you find documentation for your operating systems directory system. You will really need to understand the basics of how your operating system stores files on the fixed media to work with the filesystem.

    Jim
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by jimblumberg
    That is not a relative path. A relative path is something like:
    Code:
    "someDirectory/yourFile"
    The example I gave is using the current working directory.
    Well, if this describes a relative path in general, than your example is a relative path :cool:


    Originally Posted by jimblumberg
    S
    C/C++ programs don't search the PATH they must be told exactly where the file exists in your directory structure.

    I really suggest you find documentation for your operating systems directory system. You will really need to understand the basics of how your operating system stores files on the fixed media to work with the filesystem.

    Jim
    I have basic knowledge about Protected Mode, paging, Real Mode and stuff. But if the file storing of an OP is so important, I'll try to find something about it.
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Originally Posted by TheHunter
    What do you mean? A path that doesn't start with "C:\" ? What would that be in Windows 7?
    First you must answer this question: Where is c.txt located?

    Is it in the same directory as your program is? If so, then no path is needed. BTW, that would be your current working directory, or simply the current directory. The current directory should be the same as where the program's executable file is located.

    If it is in a directory near the current directory, then you could use a relative path.

    If it is somewhere entirely different, then using an absolute path as you tried would be in order.

    In writing path names, be advised that the backslash ( \ ) is the escape character, which means that it is used inside of string literals to give the following character or character sequence a different meaning. You've already used it no doubt to make 'n' into '\n', the newline. Or you could use it to allow a double quotation mark to be part of a string as in "He said, \"Aye!\", and then turned to." Without escaping the quotation marks, that could not exist as a string.

    You escape the backslash in a path thus: "c:\\test".

    But also, you are opening the file for reading only, but then you try to write to it. You need to open the file for both reading and writing. From the man page:
    Code:
    The argument mode points to a string beginning with one of the following sequences (possibly followed by additional characters, as described below): 
    
    r 
    Open text file for reading. The stream is positioned at the beginning of the file. 
    
    r+ 
    
    Open for reading and writing. The stream is positioned at the beginning of the file.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    Thank you for your explanations! With the r+, the fprintf() function seems to work now... if I remove the fgetsc() function, that is. For some reason they refuse to work together.

IMN logo majestic logo threadwatch logo seochat tools logo