File path to present dir. C++ - 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.

Related

Unable to Open .txt File

std::ifstream infile;
infile.open("example.txt");
if (!infile.is_open()) {
std::cout << "can't open" << std::endl;
}
I read this: unable to open file stream c++ and have tried using the full path infile.open("~/projects/example.txt");
The text file is in the same folder where the .cpp / .hpp files reside.
What else could be the problem?
try "realpath ~/projects/example.txt", which will show you the real path of your file, and then use this real path in your cpp
I once had the same problem on Windows. The problem was that the actual file name was "example.txt.txt", but Window Explorer was configured (by default) to hide file extensions.
A simple solution is to use c++17 filesystem library. For example, current_path will return what its name promises. Or write a simple function that will list the current path contents (see the example on cppreference, https://en.cppreference.com/w/cpp/experimental/fs/directory_iterator ) . Or create a file of some similar name, e.g. __example__.txt and see where it was created and under which name it is reported by your filesystem manager.
If this is Linux, try running your program under strace (strace myprog myoptions).

I am having trouble opening a .txt file in my C++ program

I have put it under the Source Files folder. I have set my working directory to $(SolutionDir)$(Configuration)\ (im not sure if that is right, i saw it somewhere) could someone help me troubleshoot.
int main()
{
cout << "program running" <<endl;
pair<int, unsigned int> mypair;
ifstream myfile;
myfile.open("numbers.txt", ios::in);
if (!myfile.is_open()){
cerr << "can't open input file" << endl;
}
else
{
cout << "file opened" << endl;
}
getchar();
myfile.close();
}
...output is: can't open input file
To figure out what directory you're pointing to, try to create a new file instead of opening one:
std::ofstream("out_test.txt");
Then you can find that file searching with File Explorer into the solution dir.
There is such thing as current directory for an application. When you run an app from VS working directory would be current one.
If you specify file name with relative path (or no path at all) OS will try to find that file relative to that directory. Where executable file and especially source file(s) are located completely irrelevant. So solution could be either set working directory to where numbers.txt is located (or move nubers.txt there), or use relative path something like foobar/numbers.txt or even ../foobar/numbers.txt etc or use absolute path.
okay I managed to figure it out..with help :) I did what Paulo M said :
To figure out what directory you're pointing to, try to create a new file instead of opening one:
std::ofstream("out_test.txt");
Then you can find that file searching with File Explorer into the solution dir.
After I figured out where the file got sent I added a new file w/ some integers to that dir and made sure the working directory was the same.I'm not sure if i had to change it? (can files be sent elsewhere?).
I had tried to do this before but I was not able to move my txt file into this directory by copy paste..so gave up. But anyhow I just right clicked/new text document/ and edited the doc with some numbers. then changed my program to open this new document.
saving file directly to the directory also worked. :) :) :)
but i am curious why I couldn't just paste it there?

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

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.

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++: Where does the ofstream class save the files to?

I moved from Windows to Mac and now I'm experiencing a problem with the file input/output classes: ifstream & ofstream.
In Windows when you run with g++/Code Blocks
ofstream out("output.txt");
out << "TEST";
out.close();
A new file "output.txt" will be created in the same directory.
However in MAC OS X, this file is created in my home directory: /Users/USER_NAME/output.txt
How can I have this file in the same directory together with the executable?
P.S. I'm using GCC and CodeBlocks. There are no projects - I'm just compiling a single source file.
The stream classes, like all other file-opening functions, use the current directory when you provide a relative path. You can control the current directory with a function like chdir, but a better solution is to use fully qualified file names. Then you remove your program's dependency on the current directory.
The file is simply created in the current working directory. Change working directory or provide full path.
The working directory is initially set when your program starts. When you start it from the command line, you inherit the current working directory from the shell. In CodeBlock, one of the project options is the execution working dir' for debug runs.
(See also http://www.gamedev.net/community/forums/topic.asp?topic_id=571206&whichpage=1&#3648738)
You'll need to provide a full, absolute path to the file you are trying to create.