delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/08/06:49:12

From: cbalciza AT mail DOT cccis DOT ro
To: djgpp AT delorie DOT com (djgpp mailing list)
Subject: file I/O (R/W) problem
Date: Mon, 08 Sep 97 10:48:09 GMT
Message-ID: <M.090897.134809.28@pm-1-22.cccis.ro>

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 -


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