delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/15/00:22:43

From: cbalciza AT mail DOT cccis DOT ro
To: dj AT delorie DOT com, eliz AT is DOT elta DOT co DOT il, csrabak AT dce03 DOT ipt DOT br, eldredge AT ap DOT net,
djgpp AT delorie DOT com (djgpp mailing list)
Subject: can't write to existing file (C++ I/O problem)
Date: Mon, 15 Sep 97 04:21:15 GMT
Message-ID: <M.091597.072115.51@pm-1-00.cccis.ro>

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 -


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