please help, can't read file, fopen

This is a discussion on please help, can't read file, fopen within the C Programming forums, part of the General Programming Boards category; Here is my very easy code, and it will not read the file I have. Yes, I have it in ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    24

    please help, can't read file, fopen

    Here is my very easy code, and it will not read the file I have. Yes, I have it in the directory that all the code is placed. I have almost 1 hour exactly to figure this out and I have to turn it in for a grade. any help is appreciated.

    I am using WxdevC++ 6.****

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE* pRead = NULL;
    
       pRead = fopen("matches.txt", "r");
    
        if (pRead == NULL) printf("\n\nFile can not be opened!!");
    
        else printf("\n\nFile opened for reading");
    
        system("PAUSE");
        return 0;
    }
    Last edited by live4soccer7; 03-16-2009 at 01:04 AM.

  2. #2
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    It works for me - try placing "matches.txt" is in the same folder as your source code, not the folder with the object file. That's what worked for me in Visual C++.

    PS you might want to put a newline after "File opened for reading" because otherwise the system pause prompt appears right after it.
    Last edited by Sharke; 03-16-2009 at 01:09 AM.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    And remember to close the file with fclose().

    The OS will most certainly close it for you after your program terminates, but it's bad style to rely on that.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    thanks for the help, how did you get it to work? What did you use to compile it? I think that is the issue honestly. I have tried wxdev and code::blocks

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    i had the file in same folder as the actual code. Thanks for the fclose(). I added that.

    It still won't read the file though. still gives me the error. how did you guys compile? Thanks

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Put it next to where the exe is, not the source file. IIRC Code::Blocks would produce a "bin" directory for exe's. For Dev-C++, if you are using single-file mode instead of project mode, it should work (putting it next to the source file).

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    I put the file in every freaking folder just incase I didn't have it in the right on. lol... I am using project mode because the actual code for my program is much much more complicated, but I can't proceed w/running it unless I can find out how to get it to read the file successfully. very frustrating. Thanks for the reply!!! much appreciated. I'll try single file mode now in code::blocks and wxdev.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    nope, still doesn't work in single file mode on wx or codeblocks....

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by live4soccer7 View Post
    nope, still doesn't work in single file mode on wx or codeblocks....
    run exe from command prompt - copy it to different folder, copy file to the same folder and try

    if it works - change temporary code to open it for writing, run - as you do regularly and see in which folder file is created - this is the workng folder of the project - you should put your file in this folder
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by cyberfish View Post
    Put it next to where the exe is, not the source file.
    What confuses me is that when I run the program from within Visual C++ it only works with the text file in the same directory as the source code but if I run the program from the command line it only works if I have the file in the same directory as the .exe.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Sharke View Post
    What confuses me is that when I run the program from within Visual C++ it only works with the text file in the same directory as the source code but if I run the program from the command line it only works if I have the file in the same directory as the .exe.
    in the VS - open the properties of the project, Debug tab - and fill the Working directory with the dir where the exe is located, then the behaviour in VS will be standard as well
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    i don't know how to run command line so I am downloading VS right now.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by live4soccer7 View Post
    i don't know how to run command line so I am downloading VS right now.
    Start -> Run... -> cmd

    use cd command to change directory and dir tosee its contents
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    24
    then what command do I actually execute my program. I have a main.c, a header.h, and a functions.c file that make up my program.

    I also got vs 08 installed and it still won't recognize or read the file. I again pasted the file into every freaking folder that could possibly be associated w/the program.

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Code:
    gcc main.c functions.c -o main.exe
    main.exe
    You may need to add your MinGW's "bin" dir to your $PATH, though.

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

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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