C++ File IO, how to specify output location - c++

I've been trying to learn File IO in C++ via tutorial webs and came across the following code.
Now, how do I specify the location of the output file? I've tried running the code and search for the location of the file but didn't work.
Thank you.
ofstream myfile ("InOutExample.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
system("pause");

If you specify the location, it will put it there.
ofstream myfile ("c:\\temp\\InOutExample.txt");
If you don't put a full path, it puts it in the current working directory.

Related

Why i can't use Windows Environment path with ofstream to write a text file?

Why i can't use Windows Environment path shortcut with ofstream to write a sample text file ?
\\ C:\Users\Me\AppData\Local\Temp\Test.txt
std::string Path = "%Temp%\\Test.txt"
ofstream myfile;
myfile.open (Path);
if (!myfile.is_open())
{
cout << "Could not create temp file." << endl;
}
myfile << "Hello World";
myfile.close();
myfile.is_open() always return false, also "%%Temp%%" and "\%Temp\%" not working.
I can get Temp path by Windows API, but i don't want to use API in this application.
Thank you
The %Temp% substitution is something done by some Windows programs, not by the C++ runtime. If you want to do this, just retrieve the environment variable yourself and build up a path. Something like this'll do it, but you'll want to add some error checking:
ostringstream tempfilepath;
tempfilepath << getenv("Temp") << '/' << "Test.txt";
ostream myFile;
myFile.open(tempfilepath.str());
...etc...

Problems with reading a .txt file

I am looking for an answer to my question, but i didn't find it in any other place.
I'm trying to read from a .txt file, that is located in the same directory as my project files.
I wrote this simple code:
ifstream file("file.txt");
std::string line;
std::getline(file, line);
cout << line;
...but unfortunately, nothing happened, not even an error or crashing.
Upon exploring a little further... even if I change the name of the txt("file") file, to the name of a file that doesn't exist, nothing happens.
What am I missing?
How do you know there were no errors? You did not check.
#include <cerrno>
and then
ifstream file("file.txt");
if (file) // is the file readable?
{
std::string line;
if (std::getline(file, line)) // did we manage to read anything?
{
cout << line;
}
else
{
cout << "File IO error";
}
}
else
{
cout << "error opening file: " << strerror(errno);
}
performs rudimentary checking.
if your error is due to opening file then provide full path to the file and check.
in your code you are reading the first line so if it is a white space then you can see nothing as output.
you must to iterate over each line until the last line (reaching the end of file EOF).
// let's say your file is "test.txt" which is located in D\\MyDB
// ifstream file("file.txt");
ifstream file("D:\\MyDB\\test.txt"); // use full path instead and check manully whether the file is there or not
std::string line;
if(file.fail())
cout << "Opening file failed!" << endl;
else
while(std::getline(file, line))
{
cout << line;
}
if it works when providing the full path then your current path is not the same as your project.
you can change the current directory using some API so if you are on windows then use: SetCurrentDirectory(path); and on linux use: chdir(sDirectory.c_str());
** I mean compilers not OS

ofstream, when will it fail instead of creating a new file?

i just started reading on how to open and edit files.
when working with ifstream, if the file doesnt exist, it wont be created.
in reference to the code below, when would the condition (!outfile) be false, as if the file doesn't exists it will simply be created by the constructor, hence always making the condition false.
int main()
{
ofstream outfile ("test1.txt");
if (!outfile)
{
cout << "cannot create file test1.txt" << endl;
return 1;
}
outfile << 10 << " " << 345.12 << endl;
outfile << "This is a short text file";
outfile.close();
system("PAUSE");
return 0;
}
One way opening an ofstream could fail is if the file in the given path exists, but you do not have the permission to write to it. Alternatively, if the file does not exist but you do not have permission to create a file in the given path, opening the ofstream should also fail.
Another failing situation could be if the files does not exist, and the underlying device does not have sufficient free space/inodes to create one.

Trying to manipulate files in c++

When using <fstream> library to open and add a stream to an existing file test.rtf and I use the following lines:
char data[100];
// open a file in write mode.
ofstream outfile;
outfile.open("test.rtf");
if (outfile.is_open()) { cout << "file is open" << endl; }
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
// write inputted data into the file.
outfile << data << endl;
And when reading it by using ifstream, the lines input are displayed correctly. The problem is the output file is not modified and lines I have added are not saved. The question might sound very stupid but it's a problem I could not resolve.
When you << to your file you are just writing to a buffer, not actually "flushing" it to the file itself. If you just close your file you should be fine.
So:
outfile.close()
Also in the future you can flush (actually write from buffer to the file) when you want to write to a file but not close it. .close() flushes then closes for you automatically.

unable to open file in C++

I am trying to open these two files and read their contents into two different arrays, but whenever I try and open them I get the unable to open file dialog? I don't see anything incorrect but I am not a strong c++ user.
std::ifstream inFile;
inFile.open("fives.txt");
if (inFile.is_open())
{
while (! inFile.eof() )
{
getline (inFile,line);
fives[loop] = line;
cout << fives[loop] << endl;
loop++;
}
inFile.close();
}
else cout << "Unable to open file";
inFile.open("search.txt");
loop=0;
if (inFile.is_open())
{
while (! inFile.eof() )
{
getline (inFile,line);
search[loop] = line;
cout << search[loop] << endl;
loop++;
}
inFile.close();
}
else cout << "Unable to open file";
The files must exist in the current directory, where the current directory is the directory from which the program was executed (not necessarily the one where the executable is saved at).
In your case, you saved the files with the resources, not with the resulting binary (I'm guessing you're running from within the VC++, by default it sets the current directory to where the binary is stored), so the program cannot find them. Use either relative path to where the resources are, or copy the files you're looking for into the directory you're running from.