fstream file opening start piont - c++

I am opening file with full path
fin.open(C:/Users/User/Desktop/MyFiles/File/file.txt);
and without
fin.open(../File/file.txt);
And both works fine :
Does that mean that C:/Users/User/Desktop/MyFiles is useless and in both ways fstream starts opening file from /File/ folder ??:
And if it is right how can I force it to start from C:/..?.(because sometimes it could not open with full path).

Related

how to correctly write path for files in the network for c++11 fstream

i need to open file that is in the network but my ifstream couldnt find the file.
when i rightclick --> properties the file the file location said is
\\LAPTOP-UDC1U1DT\Users\Public\Documents\log.txt
but this doesn't work in ifstream because of compiler errors.
\\\\server\\share\log.txt
i then tried this as said in another question, although this has no errors but the program couldn't find the file.
can somebody teach me how to correctly write the path for c++ so that fstream can find the file?
the text file by the way is shared using ad hoc.
thank you.
If you want to open or read/write to a file in C++, you have got to use a double backslash instead of a single one. Moreover you have to use the full path because it may happen that your file is not located in the same directory as your .exe is.
Maybe try something like this:
ifstream inputFile;
inputFile.open("C:\\folder1\\folder2\\text.txt", ios::in);
This should also work if you want to get a file out of your server which is your running system is connected to.

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.

Reading from folder in working directory C++

I want to read from a text file which is saved in a folder in my working directory. But unless the text file is in the working directory, it wont open.
Im using a line like this one. what should it be changed to?
ifstream myfile ("./folder/example.txt");
If you are on Windows, use this format to open the file.
ifstream myfile ("C:\\Book\\file.txt") ;
Supposing your file is in Book folder of C Drive.
First write the Directory and then the subsequent folders with double slashes in between till you reach the text file.
Though it is better if you put in your project directory itself, using this.
ifstream myfile ("file.txt") ;
You can access the directory your program is, by simply writing
ifstream myfile ("example.txt");
If example.txt is in the same directory as your program.
Note that this may not work if you can't save in this directory.

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.