Thread: Why am I unable to open the files?

  1. #16
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Well I've changed it back to this, which is working fine:

    Code:
    ifstream file("FTSE_from_1984_training.csv");    // File for TRAINING
    if(file.fail())
    {
    	cerr << "Input file 'training' problem!" << endl;
    	return 1;
    }
    
    ifstream test_file("FTSE_from_1984_testing.csv"); // File for TESTING
    if(test_file.fail())
    {
    	cerr << "Input file 'testing' problem!" << endl;
    	return 1;
    }
    If anybody has any suggestions on how to let the user define the files please let me know.

  2. #17
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    string training, test;
    cout << "Enter training and then test path" << endl;
    cin >> training;
    cin >> test;
    
    ifstream file(training.c_str());    // File for TRAINING
    if(file.fail())
    {
    	cerr << "Input file 'training' problem!" << endl;
            cout << training << endl;
    	return 1;
    }
    
    ifstream test_file(test.c_str()); // File for TESTING
    if(test_file.fail())
    {
    	cerr << "Input file 'testing' problem!" << endl;
            cout << test<< endl;
    	return 1;
    }
    As you did in the beginning

  3. #18
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Just to be 100% clear: if my file names are XOR and XOR_Test (located in a file called MyFile on the desktop) what should I put in when the console prompts me?

  4. #19
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    xor is a reserved word.

    I am definitely entering correct names as well.
    (I am not putting any quote marks around the name or anything like that, just straight:
    XOR
    which is the name of the file)

    So what could be wrong?
    pick another name to see if that is what the problem is.

  5. #20
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Why do you keep files on your desktop? Nah, disregard that.
    It would depend on the location of the executable if you want to put relative path, or not at all if you want to put absolute path. i.e. "c:\Documents and Settings\USERNAME\Desktop\my_file.test" would be absolute path.
    "my_file.test" would be relative path (if your application is run from the same directory as the file is, it will work just fine).

    When using the debugger, did you set a breakpoint before opening the file, and looked up what was actually in the string? Depending on the debugger and/or IDE you may have hard time seeing what is in std::string object, but you can quite easily "see through" string.c_str(), which returns const char*.

  6. #21
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Quote Originally Posted by kryptkat View Post
    xor is a reserved word.
    It is a good point, however C++ is case sensitive and the file name is XOR. Used a different one anyway and still no luck.

  7. #22
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Quote Originally Posted by Xupicor View Post
    Why do you keep files on your desktop?
    So where is it desirable to keep them instead?

    As for the other comment Im so new to debugger that half the time have no idea what it means when it points out mistakes. So in short no.

    In any case what could be wrong with what I am coding? As I mentioned what I changed to works fine and I did not relocate the file anywhere, it is still in the same folder as my program.

    Could someone please tell me what I am supposed to put into console when it prompts for the name? If the name is XOR then I put in XOR or some path to it?

  8. #23
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    About the desktop, well, I tend to keep only shortcuts on the desktop. I keep my projects at "E:\Projects" and every project in separate directory, of which some are grouped together. I just dislike having actual files and directories shown on my desktop, but it is just a preference.
    If you like your files flying around the desktop, then do as you please.

    Is this the code we're talking about? Why am I unable to open the files?
    I'd ask you if you are sure the file is named "XOR"? Maybe "XOR.txt" or "XOR.csv"? No extension at all?

    Maybe try this code to see where the application "thinks" it is.
    Code:
    #include <cstdlib>
    
    int main() {
        system("cd"); // if you are on windows
    //    system("pwd"); // if you are on linux, or have pwd on windows ;)
    
        return 0;
    }
    Is it the correct directory?

  9. #24
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Oh! Yes it is a csv file!! So I am supposed to type in the console XOR.csv!!!
    Aaaaaa you are a star! Well done! Never underestimate the shortcomings of a novice!!!! lol
    Really much obliged, thank you very much

  10. #25
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    You probably should turn the "Hide extensions for known file types" option off in your Explorer. I thinks it's the first thing I ever do on a Windows machine. (Tools->Folder Options->View [Advanced settings]).

    Otherwise, I'm glad I could help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help: Multi-threading and Synchronization
    By Anom in forum C Programming
    Replies: 7
    Last Post: 12-08-2009, 05:34 PM
  2. open and read bmp image files
    By cnewbie85 in forum C Programming
    Replies: 2
    Last Post: 05-19-2009, 01:36 AM
  3. Unable to open a file for reading
    By raghuveerd in forum C Programming
    Replies: 16
    Last Post: 06-18-2007, 11:47 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. open directory and copy files
    By 5n4k3 in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 09:49 AM

Tags for this Thread