Program wont write to file
-
/* Ok Here is the jist of what this program is supposed to do: This program checks "database.txt" for the text "New String" which is in the string name "string". If found it will say so. If it goes through the entire file without finding the text, it is then supposed to say it has found a new string and then append the text "New String" to the end of the file. This code is error free. Only problem is that it doesnt write the text to the file, anywhere. If someone can tell me why I'd love to know, because I cannot figure out why. Thanks */ #include #include #include using namespace std; int checkdatabase(int x, std::string current, std::string string) { fstream stream("database.txt", ios::in | ios::out | ios::app); if (!stream) { cout << "Cannot Open database" << endl; return 2; } while (std::getline(stream, string)) { x++; if (current == string) { std::cout << "string #" << x << " Found: " << string << std::endl; stream.close(); return 0; } } x++; std::cout << "***New String Found*** " << "#" << x << ": " << current << std::endl; stream << "String" << endl; stream.close(); return 0; } int main() { std::string current = "New String"; std::string string; int x = 0; checkdatabase(x,current,string); return 0; }
-
/* Ok Here is the jist of what this program is supposed to do: This program checks "database.txt" for the text "New String" which is in the string name "string". If found it will say so. If it goes through the entire file without finding the text, it is then supposed to say it has found a new string and then append the text "New String" to the end of the file. This code is error free. Only problem is that it doesnt write the text to the file, anywhere. If someone can tell me why I'd love to know, because I cannot figure out why. Thanks */ #include #include #include using namespace std; int checkdatabase(int x, std::string current, std::string string) { fstream stream("database.txt", ios::in | ios::out | ios::app); if (!stream) { cout << "Cannot Open database" << endl; return 2; } while (std::getline(stream, string)) { x++; if (current == string) { std::cout << "string #" << x << " Found: " << string << std::endl; stream.close(); return 0; } } x++; std::cout << "***New String Found*** " << "#" << x << ": " << current << std::endl; stream << "String" << endl; stream.close(); return 0; } int main() { std::string current = "New String"; std::string string; int x = 0; checkdatabase(x,current,string); return 0; }
Hi, I took tried ur code. Seems that fstream is havin some prob. I tried with a different stream for writing after closing the current stream and it worked !. ie added the following code after stream.close(); fstream stream2("database.txt", ios::in | ios::out|ios::app ); stream2 << current << endl; stream2.close(); This means that the first stream is having some problem Sujan
-
/* Ok Here is the jist of what this program is supposed to do: This program checks "database.txt" for the text "New String" which is in the string name "string". If found it will say so. If it goes through the entire file without finding the text, it is then supposed to say it has found a new string and then append the text "New String" to the end of the file. This code is error free. Only problem is that it doesnt write the text to the file, anywhere. If someone can tell me why I'd love to know, because I cannot figure out why. Thanks */ #include #include #include using namespace std; int checkdatabase(int x, std::string current, std::string string) { fstream stream("database.txt", ios::in | ios::out | ios::app); if (!stream) { cout << "Cannot Open database" << endl; return 2; } while (std::getline(stream, string)) { x++; if (current == string) { std::cout << "string #" << x << " Found: " << string << std::endl; stream.close(); return 0; } } x++; std::cout << "***New String Found*** " << "#" << x << ": " << current << std::endl; stream << "String" << endl; stream.close(); return 0; } int main() { std::string current = "New String"; std::string string; int x = 0; checkdatabase(x,current,string); return 0; }
Based on Sujan's comment, could it be that
getline()
is positioning the file pointer past the end of the file such that the append operation fails?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Hi, I took tried ur code. Seems that fstream is havin some prob. I tried with a different stream for writing after closing the current stream and it worked !. ie added the following code after stream.close(); fstream stream2("database.txt", ios::in | ios::out|ios::app ); stream2 << current << endl; stream2.close(); This means that the first stream is having some problem Sujan
Thank you sujan, that did work. :) That makes me wonder why the first stream didnt work? If it did get corrupted like you said. Funny thing, before I posted this I did try where I closed the original stream and then reopened it again and it still didnt work. Go figure!