My program works for cout, but when I want to overwrite to the file I originally opened, nothing happens. I don't want to use append, just overwrite. I basically want to take my encoded file, read it, and print the results to the computer, which I know how to do, and back to the text file that had the encoded text (it is unscrambled when I send it back)
int main ()
{
cout << "programmer: darren sandusky " << endl;
cout << "this program decodes input text file and sends it to and output file and console " << endl;
ifstream fin;
fin.open ("secret.txt"); //reads the scrambled text. Unscrambled should show
//up back here as well after the program runs
if (!fin.good()) throw "I/O error";
ofstream fout;
fout.open ("secret.txt"); //I added this and the output shows nothing
if (!fout.good()) throw "I/O error";
while (!fin.eof())
{
int i;
string a;
fin >> a;
fin.ignore (1000, 10);
for (int i = 0;i < a.length(); i++)
a[i]--;
fout << a << endl; //this should print unscrambled to secret.txt
cout << a << endl; //this works. It prints the unscrambled version to console
^i opened the file, and it still prints to the console just fine. It doesn't overwrite the decoded text though in the file; it leaves what was originally there.
I don't really use fstream alot but I'm going to guess its because you're both reading and writing to the same file, try changing the filename for fout to secret2.txt
I would, but the assignment says write to the same file..its tricky because that what screws with the logic...Its like trying to do too much to one file at the same time..
^i tried that but nothing doing. now, if you use ios :: app, that works, but it appends, not overwrites...Also, I do the encoder everytime, so I know that there is stuff in secrets.txt to begin with..
In that case how about you open them separetely, reading all of the strings into an array of strings, decoding and then writing them back to secret.txt
thanks Warnis! I will try that and see if it works. reading/writing at the same time was the first thing that came to mind, so I will try what you suggested.