File I/O (#18943)

This is a discussion on File I/O (#18943) within the C++ Programming forums, part of the General Programming Boards category; I have had some troubles with trying to get a program to read a file without putting the whole path ...

  1. #1
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

    File I/O (#18943)

    I have had some troubles with trying to get a program to read a file without putting the whole path in for it.

    Code:
    	std::ifstream file( "Runes.txt" , std::ios::in );
        if (!file.is_open())
            std::cout<<"Error opening file";
        else
    Doesn't work for me, but putting C:\\My Documents\\RuneParse\\Runes.txt in the quotation marks does work, what am I doing wrong.

    I am using Dev-C++, if that helps.

    (PS - This was done before i found out about using namespace std so that is why all the std:: are in there, I have yet to remove them in this program)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you aren't doing anything "wrong". If you don't specify the whole path then the open function looks for the file only in the current working directory.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    The file is in the same directory that the exe is in, so I would assume that would make that directory the active directory

    EDIT: Nevermind, when I run the exe by clicking on it, it works fine, if i run it from the compiler it does not work.
    Last edited by Wraithan; 11-27-2005 at 07:52 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    That doesn't mean the current working directory is the same as where the .exe is located. If you execute the program from within the IDE of a M$ compiler, such as VC++ 6.0 or newer, the current working directory is not where the .exe is located, but where the project files are located (normally the parent of the directory that contains the .exe).

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Wraithan
    The file is in the same directory that the exe is in, so I would assume that would make that directory the active directory
    This is right if you call the program from the commandline. If you call it by selecting run from windows explorer a different working directory might be set.
    Kurt

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    If I understand corectly, this should allow the user to put in the path to the file they want to load. For some reason I can't get this to work though, am I doing someting wrong?

    Code:
    string filename;
    cout<<"TodesRunes could not find your ATMA dump"<<endl<<"please specify the path to the txt file."<<endl;
    cin>>filename;
    cin.ignore();
    ifstream file( filename.c_str() , ios::in );
                        
    if (!file.is_open())
        cout<<"The file does not appear to be here!";

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Another question, inline functions, and how to make it so a funtion outside can access the loaded file. Such as "file" in the above example.

    When I try to do as I explain above I get:

    Quote Originally Posted by compiler
    c:\docume~1\owner\mydocu~1\parsing\todesr~1.cpp:80 : initializing non-const `ifstream &' with `const char *' will use a temporary
    c:\docume~1\owner\mydocu~1\parsing\todesr~1.cpp: In method `ifstream::ifstream(const ifstream &)':
    C:\DEV-C_~1\Include\G__~1\streambuf.h:128: `ios::ios(const ios &)' is private
    c:\docume~1\owner\mydocu~1\parsing\todesr~1.cpp:80 : within this context
    c:\docume~1\owner\mydocu~1\parsing\todesr~1.cpp: In method `fstreambase::fstreambase(const fstreambase &)':
    C:\DEV-C_~1\Include\G__~1\streambuf.h:128: `ios::ios(const ios &)' is private
    c:\docume~1\owner\mydocu~1\parsing\todesr~1.cpp:80 : within this context
    c:\docume~1\owner\mydocu~1\parsing\todesr~1.cpp: In method `istream::istream(const istream &)':
    C:\DEV-C_~1\Include\G__~1\streambuf.h:128: `ios::ios(const ios &)' is private
    c:\docume~1\owner\mydocu~1\parsing\todesr~1.cpp:80 : within this context
    Code:
    inline void loadfile(ifstream& file("runes"))
    {
        ifstream file( "Runes.txt");
        if (!file.is_open())
            ifstream file( "runes.txt");
            if (!file.is_open())
                ifstream file( "rune.txt");
                if (!file.is_open())
                    ifstream file( "Rune.txt");
                    if (!file.is_open())
                        {
                        string filename;
                        cout<<"TodesRunes could not find your ATMA dump"<<endl<<"please specify the path to the txt file."<<endl;
                        cin>>filename;
                        ifstream file( filename.c_str());
                        }
                        if (!file.is_open())
                            cout<<"The file does not appear to be here!";
    }
    Code:
    int main ()
    {
        int el = 0, eld = 0, tir = 0, nef = 0, eth = 0, ith = 0, tal = 0, ral = 0, ort = 0, thul = 0,amn = 0,
            sol = 0, shael = 0, dol = 0, hel = 0, io = 0, lum = 0, ko = 0, fal = 0, lem = 0,pul = 0, um = 0,
            mal = 0, ist = 0, gul = 0, vex = 0, ohm = 0, lo = 0, sur = 0, ber = 0, jah = 0, cham = 0, zod = 0;
        loadfile();
        cout << "Loading your ATMA readout and parsing now" << endl;
    	string line;
    
    
        while (getline( file , line ))
    	{
            if (line.empty() || (line == "\r")) continue;
            int colon_pos = line.find(':');
            string rune = line.substr(colon_pos+2,line.size()-colon_pos-7);
            if (rune == "El")
                el++;
            else if (rune == "Eld")
                eld++;
            else if (rune == "Tir")
                tir++;
            else if (rune == "Nef")
                nef++;
            else if (rune == "Eth")
                eth++;
    The main continues on, but I think I included the parts that would help people to help me.
    Last edited by Wraithan; 11-28-2005 at 02:16 AM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    inline void loadfile(ifstream& file("runes"))
    The above line is wrong -- remove the string literal.
    Code:
    inline void loadfile(ifstream& file)

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    When I correct it, I get:

    82 c:\docume~1\owner\mydocu~1\parsing\todesr~1.cpp
    declaration of `file' shadows a parameter

    which is the line that has

    Code:
    80 inline void loadfile(ifstream& file)
    81 {
    82     ifstream file( "Runes.txt");
    83     if (!file.is_open())
    84         ifstream file( "runes.txt");
    85         if (!file.is_open())
    86             ifstream file( "rune.txt");
    87             if (!file.is_open())
    88                 ifstream file( "Rune.txt");
    89                 if (!file.is_open())
    90                     {
    91                     string filename;
    92                     cout<<"TodesRunes could not find your ATMA dump"<<endl<<"please specify the path to the txt file."<<endl;
    93                     cin>>filename;
    94                     ifstream file( filename.c_str());
    95                    }
    96                     if (!file.is_open())
    97                         cout<<"The file does not appear to be here!";
    98 }
    Code:
    int main ()
    {
        ifstream file;
    
        loadfile(file);
    I had to intialize the variable in main, and this seemed to fix 99% of my errors, but this last one has me caught.

    EDIT: I found out why it is saying that, I have it declared twice, is the reason I think, is there a way around that?
    Last edited by Wraithan; 11-28-2005 at 04:49 AM.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you get that error because file is being declared twice -- as a parameter and in the line immedicately following it. Correct like this
    Code:
     inline void loadfile(ifstream& file)
     {
           file.open( "Runes.txt");

  11. #11
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Thanks for all that help Ancient Dragon, one last question (I hope) using user input for the path to the file always fails for me. The program just quits when you type something in.

    Code:
                    if (!file.is_open())
                        {
                        string filename;
                        cout<<"TodesRunes could not find your ATMA dump"<<endl<<"please specify the path to the txt file."<<endl;
                        cin>>filename;
                        cin.ignore();
                        ifstream file( filename.c_str());
                        }

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    cin>>filename does not accept spaces in the filename. If the path contains spaces then use getline()

    When filename is std::string
    Code:
    std::string filename
    getline(cin,filename);
    when filename is character array
    Code:
    char  filename[255];
    cin.getline(filename,sizeof(filename));
    And you don't need cin.ignore() after inputting strings. That will cause the program to stop and wait for more user input!

  13. #13
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    It is no longer just exiting out when I am using user input. But even if I put the whole path, case sesitive, it does not find the file.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    then the file is probably not where you think it is, or you may be misspelling it. MS-Windows operating system file names are case insensitive.

  15. #15
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I severly doubt it is me misspelling or not knowing where the file is, since I have been going through cmd and i can see the complete path.

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

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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