Reading from folder in working directory C++ - 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.

Related

C++ How to open an input file with another directory?

My project is being worked in through my school's intranet.
I basically am running the project through Windows Powershell by using ssh login to the intranet.
The data file is located at: /user/name_of_teacher/data/file_name.dat.
I have tried:
ifstream infile;
infile.open("/user/name_of_teacher/data/file_name.dat");
But it doesn't work.
If you want to write in this file:
ofstream file("C:/user/name_of_teacher/data/file_name.dat");
file << text;
If you want to read in this file:
ifstream file("C:/user/name_of_teacher/data/file_name.dat");
file >> text;
Remember to write:
using namespace std;
at the start of the file cpp but after the libraries.
The principle error was that you had to use the absolute location:
C:\Users ecc.

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.

Path to file used as environment variable

My C++ code for outputting content to a file is:
fstream myfile;
myfile.open("file.txt", ios::in);
if(!myfile.is_open()) cout << "error";
I want my code to function in the following way:
If file.txt is located in the same directory as the built executable or the cpp file, then my code should open the file.
Currently this is not the case. When I checked the location of the current working directory with getcwd it showed /User/myusername instead of /User/myusername/Desktop where all the files actually are located.

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.

Using ifstream for all .txt files in directory

I am wondering how to read in all files in a directory (.txt) one by one without knowing their file names?
I am familiar with the following simple case and its derivatives:
ifstream inTestData;
string inputFile("filename.txt");
inTestData.open(inputFile.c_str());
but I wasn't sure if you didn't know the file name and you had multiple files in the directory, of which you want to read all of them (assuming you put all of the .txt's in that directory).
Any thoughts?
Thanks!