program won't 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 append the tex to
the file, anywhere. If someone can tell me why I'd love to know,
because I cannot figure out why. Thanks */
#include <fstream>
#include <iostream>
#include <string>
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;
}
|