Opening a file in C++ outside of the working directory - c++

I've got a program that is going to have several resource files that the user can put somewhere on the computer that isn't in the same folder as the executable. How do I get open those files?
I've found lots of answers saying that the reason things aren't working is that the file isn't in the working directory. I've tried providing fully qualified paths:
ifstream str;
str.open("/home/millere/foo.txt")
but that was unsuccessful. I know the path was correct (copy and paste). I can't find any documentation on it, but I assume it has to be possible. (vim ~/foo.txt from anywhere other than ~ works, for example).

Assuming you meant to use ifstream instead of iostream, your code is correct. ifstream can use a path to a file as well as the name of a file in the working directory.
No exceptions are thrown if the file does not exist, but the fail bit is set. You should check for this before trying to do anything with the stream.
std::ifstream input("/home/bob/stuff.txt");
if (!input) std::cerr << "Could not open the file!" << std::endl;
else
{
// ...
}
If you still cannot extract data from the file, the problem is somewhere else in your code.

I had the same issue and quickly noticed that, open when trying to get from a difference folder, had a different source directory (if using Cmake, the one that was specified by the cmake). You can find out, what the ouput/input source directory is by doing
system("ls")
or
system("dir")
on windows to show the content of the current ouput/input directory.

Related

Ofstream creates hidden(?) files (while .open) on Ubuntu

I have the following problem. I would like to open and save a string to file.
ofstream file("file");
file << "example" << endl;
file.close()
Rather simple stuff. The problem is... the code above doesn't seem to create any files in my project directory. I have tried to use "locate" in terminal to find my file - withoud any results. What is interesting - I can open the file using ifstream and read from it, without any particular problems.
What the heck is going on here? Where should I look for the file created?
P.S. On Windows everything works 100% fine.
in my project directory
Where did you read that this is where to look? We should collaborate and correct that resource.
I have tried to use locate in terminal
It probably hasn't been indexed yet.
You need to look in the current working directory of the terminal that started the program.

ifstream will not open

The file exists in the directory, and I've tried running Visual Studios in Administrator Mode. However, ifstream can not find the file I give to it.
Here is the code I am using:
std::ifstream instream;
instream.open("appdata.txt");
if (!instream)
{
std::cout << "Could not find appdata.txt!";
}
But I am always greeted with Could not find appdata.txt! when I run the program.
Here is a picture of my directory, for proof that I have it spelled correctly and it exists.
So, my question is, am I missing something so glaringly obvious that I am glazing over it each time I look? I can not figure out for the life of me why instream can not open appdata.txt.
This is a problem with current directory being set to something else than a dir where your file is (usually your home folder if executing from explorer).
Try executing the program from command line from directory where your file is.
EDIT
If you want to set the working directory to some specific location, check this: https://msdn.microsoft.com/en-us/library/aa363806.aspx
Add the file by right clicking on project name on visual studio interface.
This will keep your file in the right directory.
If you want to add in the directory by yourself, first add a file using the method I said above and the find which is the folder you should keep so that you can use that file by mentioning just the file-name. And then you can add your files in that folder.

File path to present dir. C++

I am opening a ofstream in a small C++ program. The file is in the same directory as the program. It works fine when i open it with full file path.
But open it with only "file.dat" doesn't work.
So my question is: How do i declare the path to a file (or just the file) without including its path? Given that it is the same directory.
if(!readTheFile("/Users/mydirect/Desktop/DV1S5U4/DV1S5U4/timelog.dat")){
cout << "Cant read timelog.dat" << endl;
}
Your code should work. The problem you're having is that you need to set your working directory since I dunno where the default is.
Product > Edit Scheme > Info > Working Directory
There you can set it. Once you've set it and placed the .dat in the same folder it'll work.
If the file really is in the current working directory you can use "timelog.dat" or, at least on POSIX systems, "./timelog.dat". From the sounds of it, the current working directory seems different from what you think it is.
BTW, note that opening a file for reading with std::ofstream won't work too well: you might want to try std::ifstream. If the file was writable when you tried to open it with an std::ofstream it will now be empty unless you passed the argument std::ios_base::app when opening the file. ... and if the file is not writable, opening it with an std::ofstream will fail.
You write that your file is in "the same directory as the program" - it sounds like what you are after is not the current directory but the directory the executable came from. You can find that out on OS X using the function _NSGetExecutablePath() - this just takes a buffer and buffer size and returns the path.

Can't open text file resource in Xcode 4 c++ tool?

I am working on figuring out how to use Xcode 4 to debug c++ projects.
I have basically copy pasted a working c++ executable that when compiled from the terminal ran fine.
However, i was thinking it might be nice to use Xcode for debugging. So I am trying to migrate the single .cpp file into Xcode as a command line tool.
I need to read in a file called numbers.txt (which I supply through a command line argument) which is located in my project directory, and then out put to a file (whose name I also specify as an argument.)
The problem I am running into is that the files that are supplied as command line arguments are failing to open.
ifstream in;
ofstream out;
in.open(argv[1]);
out.open(argv[2]);
I have checked to make sure that the arguments are being properly passed and are named correctly. The ifstream in is being supplied with `numbers.txt', which I want to open a text file that I already have.
However when I check to make sure the ifstream is open:
if(in.is_open() == false){
cerr << "Unable to open input file" << endl;
return 1;
}
I get the error.
I suspect this has something to do with how Xcode organizes the project.
my numbers.txt file is just sitting in the Xcode project folder, and I have only one .cpp class and one product, the executable.
anyone know what I am missing here?
The executable built by Xcode is in a different folder than the project. Passing in the name of the file without an absolute path before it will cause the executable to look for it in the wrong place, which is why it can't be found. Some of the possible solutions are to include the file as part of the build process (so it ends up in the same directory as the executable) or to pass the file to be opened by its absolute path. There are other ways to solve the problem, too, but hopefully that should be enough to get you started.
Old thread, but i have faced the same problem now, and it is easy to solve. Just copy the file in the build phase.
Here is an screenshot of the final result (note the destination, subpath and checkbox):

C++ program infile won't open in xcode?

alt text http://img638.imageshack.us/img638/5731/screenshot20100613at121.png
Why does the c++ program produce the error shown? I'm especially confused since outfile opens without error yet infile displays the error? Both are defined in xcode exactly the same!! I've altering the "path type" setting without success. The open on infile always fails! Any suggestions would be very much appreciated!!
For those who responded thanks but as you can see but infile and outfile exist and are in the same location:
alt text http://img709.imageshack.us/img709/9316/screenshot20100613at123.png
In Xcode, expand the "Targets" node and find your target. Given what I've seen, it'll be the only one there.
Next, right-click on your project and add a new "Copy Files" build phase. When the dialog box comes up, make sure you set the Destination to "Products Directory".
The new build phase will appear beneath your target with whatever name you gave it. Drag your in_file and out_file there.
Compile and run.
In your current case out_file would be created even if it doesn't exist (because you're using std::ofstream).
in_file, on the other hand, has to exist, and (I guess there is no such file in the directory with created binary), hence an error is produced.
Did you try launching your compiled application with the file in the same folder where the binary file is?
Probably there is no file named "in_file" in the program's working directory, so it can't be opened for reading.
For outfile this doesn't matter since it is opened for writing and if it doesn't exist yet it will just be created.
The directory listing you posted doesn't show where the compiled executable is, but probably it will be somewhere in the build directory. Probably this is then also its working directory and the place where the input file would need to be. (Look for the out_file the program creates when it is run, it will be created in the working directory, the same directory where it searches for in_file. And it is not the directory you posted the listing of, the out_file there is too old.)
Turns out you have to specifically create the file here:
Project Path/build/Debug
apparently you can't just define it directly in the xcode project