Mail Archives: djgpp/1997/09/15/00:22:43
Problem:
- (existing) file content discarded when trying to
(C++) read and write.
Purpose:
- updating records into an existing database
Goals so far:
- works if using C style fopen with 'r+'
- fails if using C++ style
ifstream f("test.txt", ios::nocreate | ios::in | ios::out | ios::binary);
IDE:
-RHIDE & DJGPP 2.01 using the following switches:
gcc -g -c test.cc -o test.o
gcc -o test.exe test.o -liostr
Input file test.txt containing 42 bytes as follows:
abcdefghijklmnopqrst
12345678901234567890
Expected output:
jihgfedcbaklmnopqrst
12345678901234567890
Actual output:
GARBAGE
Program:
// test program
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
ifstream f("test.txt", ios::nocreate | 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 -