Thread: Why am I unable to open the files?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41

    Why am I unable to open the files?

    Please tell me why this is failing to open the files:

    Code:
    string trainingFile;
    	string testingFile;
    
    	cout << "Please specify the file you would like to use for TRAINING: " << endl; 
    	cin >> trainingFile;
    
    	cout <<"Please specify the file you would like to use for TESTING: " << endl;   
    	cin >> testingFile;
    
    	ifstream file;				     // input file stream
    	file.open(trainingFile.c_str()); // c_str() converts file name into a form open() can handle
    	if(file.fail())
    	{
    		cerr << "Input file 'training' problem!" << endl;
    		return 1;
    	}
    
    	ifstream test_file;
    	test_file.open(testingFile.c_str());
    	if(test_file.fail())
    	{
    		cerr << "Input file 'testing' problem!" << endl;
    		return 1;
    	}

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No idea. Try explicit paths. If that works, they it's probably you who enter incorrect implicit paths to the files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Sorry I don't know what explicit and implicit paths means?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Explicitly means a full path, that is, for Windows, example C:\MyFile.txt.
    Implicitly means a relative path, such as MyFile.txt.
    I should have used full/relative instead of explicitly/implicitly. Sorry.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Can I open 2 files at the same time? Maybe the first one needs to be closed before I can read in second one?

    It works fine if I say:
    Code:
    	ifstream file("y=x2_positive.csv");
    	if(file.fail())
    	{
    		cerr << "Input file problem!" << endl;
    		return 1;
    	}
    
    	ifstream test_file("testing_y=x2_positive.csv");
    	if(test_file.fail())
    	{
    		cerr << "Input file problem!" << endl;
    		return 1;
    	}
    But would have liked to specify at runtime....

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Quote Originally Posted by Elysia View Post
    Explicitly means a full path, that is, for Windows, example C:\MyFile.txt.
    Implicitly means a relative path, such as MyFile.txt.
    I should have used full/relative instead of explicitly/implicitly. Sorry.
    Im too much of a novice so either would have actually been new terminology to me! (I think you mean for me to have done what I posted above?). Thank you

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no limit on how many files you can have open at once (language wise). 2 should be no problem. The most likely cause is that it cannot find the files you are specifying.
    As for full/relative path, the thing is this:
    - Every file has a path associated with it. For example: C:\MyDirectory\MyFile.txt. Nothing difficult here. This is a full path.
    - Now consider that, say, your executable is already located in C:\MyDirectory. Then, when your program starts, its working directory is C:\MyDirectory. So then you can say MyFile.txt, which is a relative path. This refers to the fact that you don't specify a full path. In this case, your relative path is added to the working directory to form the full path. In this case, it becomes C:\MyDirectory\MyFile.txt.
    Last edited by Elysia; 09-26-2010 at 03:20 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    242
    i think elysia is talking about what i call absolute and relative paths. absolute (explicit) means that you spell out the entire path all the way from the root (e.g., in Windows, c:\Users\...) relative (implicit) allows you just to put the filename if it's in the directory you're calling it from "my_file.txt" as well as using ../ to go up one level, etc.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    OK thank you. I have checked and the files are located in the same folder as the program.

    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?

  10. #10
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Can you suggest another way please? (For the user to specify the files).

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) Does full paths work?
    2) Make sure the files are not open in any program. If they are or you are unsure, you can use Unlocker or Lock Hunter to unlock them. Then try again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There is nothing wrong with your first posted code and that is what you should do to get the paths in runtime!

    You might want to add a
    Code:
    cout << trainingFile << endl;
    in your error part along with cerr so you can print on the screen the actual path its trying to open. Then make sure that
    1) The files exist
    2) The files are not open/used by another process

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please do no promote such ridiculous debugging statements. Instead suggest a debugger.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    So you want me to specify the full directory when console prompts for the file name?
    Sorry not sure how to test it. But I have just used the file when it was hard coded into the program (as posted above).

    The files are definitely not used anywhere else.

  15. #15
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    I am using the debugger and it fails on the first file.

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