delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/09/22:34:56

Date: Tue, 9 Sep 1997 19:33:42 -0700 (PDT)
Message-Id: <199709100233.TAA28497@adit.ap.net>
Mime-Version: 1.0
To: Cesar Scarpini Rabak <csrabak AT dce03 DOT ipt DOT br>, cbalciza AT mail DOT cccis DOT ro,
djgpp AT delorie DOT com (djgpp mailing list)
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: file I/O (R/W) problem

At 09:06  9/9/1997 -0300, Cesar Scarpini Rabak wrote:
>At 21:30 08/09/97 -0700, Nate Eldredge wrote:
>>At 10:48  9/8/1997 GMT, cbalciza AT mail DOT cccis DOT ro wrote:
>>>how can I open a file for both reading and writing without
>>>erasing the previous file content ?
>>I don't think rewriting a file in place works. The preferred method is to
>>write the new version to a temporary file, then replace the original with
>>it. A good example is the `utod' and `dtou' programs that come with DJGPP;
>>find their source in the library source zip.
>>
>
>Nate,
>
>The original mail still did not arrived at my account, so the comment is
>only about the snippet quoted.
Here's his code.
#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;
}
He's trying to reverse a file in place.
>I'm affraid that what the original poster intends to do is feasible with a
>call to fopen with the "r+" flag.
You're quite right. My mistake. The only question is, what is the equivalent
of that in ios flags? I suspect that's his problem.

The other problem is the constant `e=10'. This means only the first 10 bytes
will be reversed.

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


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