I'm trying to write a function that automatically formats XML-Strings; but I'm already failing when I try to read text from a file and write it into another one.
When I use my function sortXMLString()
bool FormatXML::sortXMLString()
{
string XMLString;
ifstream fin("input.txt");
fin.open("input.txt", ios::in);
ofstream fout("output.txt");
fout.open("output.txt", ios::out);
if (fin.is_open() && fout.is_open())
{
if (fin.good()) cout << "good" << endl;
if (fin.fail()) cout << "fail" << endl;
if (fin.bad()) cout << "bad" << endl;
while (getline(fin, XMLString))
{
//TODO: Formatting
fout << &XMLString << endl;
}
fin.close();
fout.close();
}
else return false;
return true;
}
I will get the output "fail", but the function never enters the while-loop. The function returns true.
It doesn't matter what I write into my input.txt (a single letter, a single number, multiple lines of text or even nothing), the failbit will always be set before getline can even be reached.
Why is this/ how can I properly read out of my file?
ifstream fin("input.txt"); will open the file with fin as stream object why calling open member function again ? same goes for fout object too.
Calling open on an already open stream fails, meaning the failbit flag is set to true.
Just open once
ifstream fin("input.txt");
ofstream fout("output.txt");
Related
I am building an input validation function that takes the input of the user and tries to open that file. and repeats if user is not entering the correct format. the correct format is:
test1.txt
My function works if I write correct format in the first run, but after the second run it keeps printing the error message although I am writing the write format to be opened. I have tried to clear the input "cin" and "filename" after taking the input but it did not work. Any ideas ?
string getFileInput()
{
string filename;
fstream file;
cout << "Please enter the name of the file: ";
getline(cin, filename);
file.open(filename.c_str());
while(!file.is_open())
{
file.clear(); file.ignore();
cout << "File name is incorrect, please enter again: ";
cin.clear(); cin.ignore();
getline(cin, filename);
file.open(filename.c_str());
}
// Extra condition. Empty file
if (file.eof())
{
cout << filename << " is an empty file." << endl;
}
file.close();
return filename;
}
I could reproduce and fix.
The problem is caused by cin.ignore(). According to cppreference:
... the next available character c in the input sequence is delim
So ignore will read the next line, up to the newline, and leave that newline alone. And the following getline can only read an empty string!
By the way, using a non opened fstream (file) for ignore and clear is at least useless and could be harmlfull because those methods are expected to be called on an open stream. And using cin.clear() is useless too and can be harmfull: if for any reason you have a read error (because you reached an end of file for example), you will consistently clear the error condition and try to read again when you should abort.
Finally, the eof condition is only set after a read returned nothing because of the end of file. It is never set when opening an empty file, nor if you could successfully read up to the end of file.
So the function should boil down to:
string getFileInput()
{
string filename;
fstream file;
cout << "Please enter the name of the file: ";
getline(cin, filename);
if (! cin) {
// test the error immediately and before using filename!
cerr << "read error: aborting...\n";
return "";
}
file.open(filename.c_str());
while(!file.is_open())
{
cout << "File name is incorrect, please enter again: ";
getline(cin, filename);
if (! cin) {
// test the error immediately and before using filename!
cerr << "read error: aborting...\n";
return "";
}
file.open(filename.c_str());
}
file.close();
return filename;
}
I'll try to be as clear as I can: Whenever I try to stream data to my file before my 'do' loop and my pointers reading and writing, my program goes nuts! It appears to be running an infinite loop.
fstream fileHandler; //Can also be done via constructor fstream fileHanlder("myData.txt", ios::out);
//fileHandler.open("myData.txt", ios::out);//Default is in AND out
fileHandler.open("test.txt", ios::in | ios::binary | ios::out);
if (fileHandler.is_open()) {
//fileHandler << "anything" <---HERE IS THE PROBLEM
cout << "The file has been opened and edited properly.";
fileHandler.seekg(0, ios::end);
streampos sizeOfFile = fileHandler.tellg();//tellg returns type streampos
fileHandler.seekg(0, ios::beg);
do{
string buffer;
fileHandler >> buffer;
cout << buffer << endl;
}while(!fileHandler.eof());
if ((fileHandler.rdstate()^ifstream::eofbit) == 0) {
fileHandler.clear();
cout << fileHandler.tellg() << endl;
}
fileHandler.close();
} else cout << "There was a problem opening the file!";
My file has nothing but a simple phrase.
EDIT: fixed the title according to new information
Thanks for any attention!
Removing the binary flag fixed it for some reason.
I'm attempting to create a repeating menu that will allow a user to re-enter a file name if the program is unable to open the file.
Right now it works correctly if I enter the name of an existing file, but if the file doesn't exist it prints the "File not found" then executes the rest of the program. I'm new to file streams and most of the code here was found through references. I'm a bit lost on what exactly is going on and what the best way to handle the situation is. Any guidance would be appreciated.
typedef istream_iterator<char> istream_iterator;
string fileName;
ifstream file;
do {
cout << "Please enter the name of the input file:" << endl;
cin >> fileName;
ifstream file(fileName.c_str());
if (!file) {
cout << "File not found" << endl;
}
} while (!file);
std::copy(istream_iterator(file), istream_iterator(), back_inserter(codeInput));
After constructing the object file will always exist, so your loop condition always fails. Change the condition to whether the file didn't open properly.
do {
...
}
while (!file.is_open())
this code will work.
do {
std::cout << "Please enter the name of the input file:" << std::endl;
std::cin >> fileName;
file = std::ifstream(fileName.c_str());
if (!file) {
std::cout << "File not found" << std::endl;
}
} while (!file);
your error was that you have 2 definition of the file variable.
the variable in while (!file) that is used is the one defined outside the do-while loop, and it is valid state is set to true by default.
In addition to #acraig5075 answer:
Writing a type then a variable name (ifstream file) is to create a new variable. Obviously you know this, but if you use the same name again in, for example, a loop, it makes a new and distinct variable.
ifstream file; // a unique variable
...
do {
...
ifstream file(fileName.c_str()); // another unique variable
...so change the usage inside the loop to:
file.open(fileName.c_str());
I have a program where I need to take in a command line argument that is the name of a text file. The program is run by ./programName file1. If for the purpose of my program I can assume file1 will always be formatted correctly, can I just do
ifstream myFile(argv[1]);
if(!myFile.good()){
cout << "Not a valid player file" << endl;
return 1;
}
to check if there is such a file, and then simply
myFile >> var1;
myFile >> var2;
myFile.close();
or should I stick with
if(myFile.is_open()){
myFile >> var1;
myFile >> var2;
myFile.close();
}
else{
cout << "Unable to open file";
}
Again I can assume the file will always be formatted correctly(i.e myFile >> var# will always work). So is there any situation in which the else statement might be called after my initial check of (!myFile.good())?
You should mainly check the input operations, to know if your file has the proper input format:
if(myFile >> var1 >> var2) {
// Everything's fine
}
else {
// WRONG FORMAT OR COULDN'T BE OPENED!
}
To know the actual error reason in the else part, you can inspect the iostate flags returned by the std::istream::rdstate() function.
If the open() failed the badbit will be set, if one of the input operations failed the failbit will be set.
I'm trying to write a simple program that will print the contents of a text file one line at a time. However, whenever I run the program i just get a blank screen. I'm certain the file I am trying to read contains text over several lines. Any help as to why this isn't working would be super helpfull.
bool show() {
string line;
ifstream myfile;
myfile.open("tasks.txt", ios::app);
while (!myfile.eof()) {
getline (myfile, line);
cout << line << endl;
}
myfile.close();
return true;
}
The problem might be that you are using ios::app with ifstream (input stream), which makes no sense.
According to this,
ios::app: All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations.
Try this:
std::string line;
ifstream myfile ("tasks.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
std::cout << line << std::endl;
}
myfile.close();
}
Did you check return value of myfile.isopen()? Perhaps the file isn't there or you don't have read permission.
Oh yes, I missed that - the append flag. Should be ios::in