Mail Archives: djgpp/1997/09/08/06:49:12
how can I open a file for both reading and writing without
erasing the previous file content ?
here's a little test program taken from an ANSI C++ manual
and applied to a file (test) containing:
This is a test program;
it should modify it to:
a si sihTtest program;
yet I only get:
TTTTT HHHHH
please reply
// test program
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
fstream f("test", ios::in | ios::out | ios::binary);
if (!f)
{
cout << "cannot open file" << endl;
return 1;
}
long e,i,j;
char c1,c2;
e=10;
for (i = 0, j = e ; i < j ; i++ , j--)
{
f.seekg(i,ios::beg);
f.get(c1);
f.seekg(j,ios::beg);
f.get(c2);
f.seekp(i,ios::beg);
f.put(c2);
f.seekp(j,ios::beg);
f.put(c1);
}
f.close();
return 0;
}
- Raw text -