delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/22/01:50:06

To: djgpp AT delorie DOT com (djgpp mailing list),
71101 DOT 1702 AT compuserve DOT com (Steve Heller)
Subject: DJGPP 2.01 C++ I/O problem
Date: Mon, 22 Sep 97 05:31:56 GMT
Message-ID: <M.092297.083156.91@noname>
From: cbalciza AT mail DOT dntis DOT ro

Problem:
- (existing) file content discarded when trying to
  (DJGPP C++) read and write. Borland C++ works fine.

Purpose:
- updating records into an existing database

Goals so far:
- works if using C   style fopen with 'r+'
- works if using C++ style fopen in Borland C++
- fails if using C++ style in DJGPP
    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:

kjihgfedcbalmnopqrst
12345678901234567890

Actual output (DJGPP):

GARBAGE. File content is discarded first,
then it reads nothing and writes it back.

Program:

// test program
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>

int main(void)
{
fstream f("test.txt", 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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019