www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/10/14:30:21

Message-Id: <1.5.4.32.19970910183209.006af414@dce03.ipt.br>
Mime-Version: 1.0
Date: Wed, 10 Sep 1997 15:32:09 -0300
To: Nate Eldredge <eldredge AT ap DOT net>, cbalciza AT mail DOT cccis DOT ro,
djgpp AT delorie DOT com (djgpp mailing list)
From: Cesar Scarpini Rabak <csrabak AT dce03 DOT ipt DOT br>
Subject: Re: file I/O (R/W) problem

At 19:33 09/09/97 -0700, Nate Eldredge wrote:
>At 09:06  9/9/1997 -0300, Cesar Scarpini Rabak wrote:

[snipped]

>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.

In C++, oring ios::in with ios::out (as done in the code above) suffices.
Now from the original question, to avoid a file get overwritten when a file
already exists, one could use the "noreplace" flag so the instanciation of
fstream will get:

 .
 .
 .
fstream f("test", ios::noreplace | ios::in | ios::out | ios::binary);
 .
 .
 .

with this, if a file doesn't exist it would be created, if this is not the
intent (it seems to be that the original poster wants to manipulate
_existing_ files) then one should use "nocreate" instead.

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

Yes. But I can't comment on this may be what all the effort of the original
poster/programmer is all about after of all.

HTH

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cesar Scarpini Rabak                      E-mail: csrabak AT ipt DOT br
DME/ASC                                   Phone: 55-11-268-3522 Ext.350
IPT - Instituto de Pesquisas Tecnologicas Fax:   55-11-268-5996
Av. Prof. Almeida Prado, 532.  Sao Paulo - SP 05508-901 BRAZIL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Raw text -


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