overwrite

Jul 21, 2011 at 12:37am
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 Jul 21, 2011 at 1:01am
Jul 21, 2011 at 12:41am
If this is all the code I'd say it's because you never open fout
Jul 21, 2011 at 12:46am
well maybe I entered it wrong but last time I included fout.open etc. it pprinted nothing...I will try again
Jul 21, 2011 at 12:49am
^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.
Jul 21, 2011 at 12:53am
Can you open a file for reading and writing at the same time with two different streams?
Jul 21, 2011 at 12:54am
Did you remember to close fout?
Last edited on Jul 21, 2011 at 12:54am
Jul 21, 2011 at 1:01am
yes. let me show you where I did it
Jul 21, 2011 at 5:02pm
bump.... Still not printing anything, and the logic seems right...
Jul 21, 2011 at 5:04pm
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
Jul 21, 2011 at 5:08pm
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..
Jul 21, 2011 at 5:08pm
Maybe try explicitly telling it to open "secret.txt" as an output stream

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