Thread: file io with dev c++

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663

    file io with dev c++

    I can't seem to get it to work. I've tried using the full path name, as well as putting the file in the same directory as the dev c++ folder, but I am unable to open the file. Here's my code:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
          
    int main()
    {
        ifstream inFile("C:\\Documents and Settings\\user 3\\My Documents\\TestData\\data.txt");
        if(!inFile)
        {
            cout<<"error opening file"<<endl;
        }
        
        system("PAUSE");
        return 0;
    }
    Last edited by 7stud; 01-05-2006 at 01:10 PM.

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Do you know why the file can't be opened? Sometimes it's more informative to get some internal error messages going rather than your own:
    Code:
    #include <fstream>
    #include <cstdio>
    #include <cstdlib>
    
    int main()
    {
      std::ifstream fin ( "C:\\Documents and Settings\\user 3\\My Documents\\TestData\\data.txt" );
    
      if ( !fin ) std::perror ( 0 );
    
      std::system ( "PAUSE" );
    }
    A good idea for any IDE is to find out exactly where the files are expected. I usually forget and end up opening a file for writing, then go find it to see where I should put my files for reading. Spaces in the path and/or directory names over 8 characters also seem to be an issue with Windows, so you may need to remove them with that tilde thingie:
    Code:
    std::ifstream fin ( "C:\\docume~1\\user 3\\mydocu~1\\TestData\\data.txt" );

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Thanks for the response.

    A good idea for any IDE is to find out exactly where the files are expected. I usually forget and end up opening a file for writing, then go find it to see where I should put my files for reading.
    That worked. I found the written file in:

    C:\Dev-Cpp\c++ programs

    I can also read files put in that directory. Can't dev c++ read files in other directories?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Dev-C++ is not a compiler it is an IDE. It uses Mingw(GCC). And yes it can read files from any directory.
    Woop?

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by 7stud
    I can also read files put in that directory. Can't dev c++ read files in other directories?
    what makes you think it's Dev-C++? try taking the binary you create out of that directory, and put it somewhere else... what happens to your file I/O now?

    you should attempt to use relative file paths whenever possible, and make sure the end user knows not to mess with the file structure you build around your program. Or if they do, have some way they can tell your program how they changed it.

    by relative file paths, I mean like this: say you have your program in a folder called myprog. in myprog is your program (myprog.exe) and your data files all neatly tucked away in a folder (called data).

    so this is how it looks:
    Code:
    + myprog
    |
    +--myprog.exe
    |
    +--+data
         +--file1.dat
         +--file2.dat
    your program would try to open up "data/file1.dat". Now imagine that you have the same file structure, except your program is in a folder called bin. you'd access those by opening "../data/file1.dat". That's telling your program to go up one directory, go down into data, and file file1.dat.

    note those are linux file paths... for windows, just use \\ in place of all the /'s


    edit: another key component I just remembered: The directory holding your binary won't always be the directory your binary is run in... for example, if you write a script to run a binary in another directory, depending on your setup, the binary won't run in the directory it's sitting in, but in the directory the script that called it is in.
    Last edited by major_small; 01-06-2006 at 01:10 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. File IO with .Net SDK and platform SDK
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2004, 10:18 AM