Trying again and again to open a file while giving wrong path - c++

I've just started working on file using C++ for the first time and I would like to write a correct program which allows me to:
Open the file giving the path;
If it fails to open (showing which error is doesn't matter), ask a new path;
Working on the file;
Close the file;
The only point I can't do is the second one. I've already tryed somethink like:
do{
cout<<"Path: ";cin>>path;
f.open(path, ios::in);
}while(f.fail());
but if I write the path of an inexisting file and then a path of the file I want to open, the program continously ask me a new path and never stops.
P.S. = Will it be different if I want to use that condition to prevent errors while creating a new file using ios::out (for example: a file name/extension which contains illegal characters)?

Clear the flags if its unsuccessful
f.open( path, ios::in ) ;
while( f.fail() )
{
f.clear();
std::cout<<"Incorrect file path, Re-Enter ";
std::cin>>path;
f.open( path, ios::in ) ;
}

Related

How to clear a file in append mode in C++

I've a file on which I require multiple operations. Sometimes I just want to append data at the end of the file, sometimes I just want to read from the file, and sometimes, I want to erase all the data and write form the beginning of the file. And then, I again need to append data at the end of file.
I'm using following code:
ofstream writeToTempFile;
ifstream readFromTempFile;
writeToTempFile.open("tempFile.txt", ios::app | ios::out);
readFromTempFile.open("tempFile.txt", ios::in);
// Reading and Appending data to the file
// Now it is time to erase all the previous data and start writing from the beginning
writeToTempFile.open("tempFile.txt", std::ofstream::trunc); // Here I'm removing the contents.
// Write some data to the file
writeToTempFile.open("tempFile.txt", std::ofstream::app); // Using this, I'm again having my file in append mode
But what I've done doesn't work correctly. Please suggest me some solution in C++. ( Not in C)
The problem with the code is:
I wasn't closing the file before I called the method open again on it.
So, close the file before you re-open it with some different permissions.

Opening a file then closing does not release file lock

I have some c++ code in a node.js function that opens a file like this and reads it into a buffer and then closes it.
ifstream inputFile(source.c_str(), ios::in | ios::binary);
inputFile.read(buffer, results.st_size);
inputFile.close();
Then I manipulate the file in some way and attempt to write it back to the same location. I get the following error code and message: "Text file busy". I know there is no other process touching the file. If I rename the file to some random name, and rename it back, then I am able to overwrite the file.

How to edit (read and then write) a file with c++ filestreaming?

For example, if I need to add a sentence to the end of a file. I have to first open the file (e.g. "a.txt"), by
ofstream outfile.open("a.txt");
But whenever I do this, I will overwrite the existing "a.txt" in the same directory. Is it possible to edit the file like first read and then write?
Thank you very much.
You want to open the file in 'append' mode. Passing ios::app to the open method causes the file to be opened in append mode.
f.open("file.txt", ios::app);
See http://www.cplusplus.com/reference/iostream/ios_base/openmode/ for other flags
Try this:
std::ofstream outfile( "a.txt", std::ios_base::app | std::ios_base::ate );
Various references:
http://www.cplusplus.com/reference/iostream/ofstream/ofstream/
http://www.cplusplus.com/reference/iostream/ofstream/open/
http://msdn.microsoft.com/en-us/library/aa277521(v=vs.60).aspx
http://msdn.microsoft.com/en-us/library/aa266859(v=vs.60).aspx

Does fstream open have trouble with larger files?

I'm trying to open /usr/share/dict/words with the following code:
fstream f;
f.open("/usr/share/dict/words");
// why is this returning false?
bool open = f.is_open();
I'm wondering why f.is_open() is returning false?
More info: when I try a smaller test file containing on the order of 20 lines f.is_open() returns true. Perhaps f.open is trying to load the entire file into memory?
It does not work because you are opening the file for reading and writing. Unless you are running as root, you do not have permissions to write to this file.
If you open it just for reading it will work:
f.open("/usr/share/dict/words", fstream::in);
The fstream.open() function is declared thusly:
void open (const char *filename,
ios_base::openmode mode = ios_base::in | ios_base::out );
i.e. it opens the file for reading and writing. Your process probably does not have permissions to open that file for writing unless you're running as root. Open it for reading only with
f.open("/usr/share/dict/words", ios_base::in);

need help to getin ifstream::open to open a file and getline from it

I am just trying to open this file and use the getline function to read from the file but I cant seem to figure out why it is not working. I have stepped through it many times and the fileOpen variable is being loaded correctly with the file im trying to open, so Im unsure on why it wont open, to use getline with it. I would just like to be able to read through the file with getline, all of this is done in a recursive function to eventually read through all the files in directories. Let me know if you need more information on what exactly im doing.
string line;
ifstream file;
string fileOpen;
bf::directory_iterator dirIter ( fullPath ); //fullPath is type bf::path, passed into the function
fileOpen = (dirIter->path().filename());
file.open(fileOpen);
getline(file, line);
The path::filename function returns the base filename. If you have a path of "foo\bar.txt", path::filename will return "bar.txt". So unless "foo\" is in the current directory, the file probably doesn't exist.
What you're more likely looking for is this:
file.open(dirIter->path().native());
Or, you can use the boost::filesystem iostream types:
#include <boost/filesystem/fstream>
bf::ifstream file;
bf::directory_iterator dirIter ( fullPath ); //fullPath is type bf::path, passed into the function
file.open(dirIter->path());