overwrite

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

}

fin.close ();
fout.close (); //fout close

return 0;
}
Last edited on
If this is all the code I'd say it's because you never open fout
well maybe I entered it wrong but last time I included fout.open etc. it pprinted nothing...I will try again
^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.
Can you open a file for reading and writing at the same time with two different streams?
Did you remember to close fout?
Last edited on
yes. let me show you where I did it
bump.... Still not printing anything, and the logic seems right...
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..
Maybe try explicitly telling it to open "secret.txt" as an output stream

 
fout.open( "secret.txt", ios::out );
^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
so track all the input as strings then? and then decode those strings, then print the decoded back to the file?
Yes, unless the assignment demanded that you read/write at the same time I think it should work just fine.
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.
do I feel dumb.. I wasn't required to do fout this whole time! fail...
Topic archived. No new replies allowed.