Thread overview
writing files: trouble
Jun 01, 2004
Ben Hinkle
Jun 01, 2004
Kris
Jun 01, 2004
Ben Hinkle
Jun 01, 2004
Vathix
Jun 02, 2004
Dennis Walters, II
June 01, 2004
I need some help...
I am trying to save a file. It (almost works)... but it happens that this
code prints some extra characters in the
beggining of the file. why?

void save(char[] filename){
  int node, offset;
  OutBuffer buffer = new OutBuffer();
  buffer.reserve(20*_xdim*_ydim);
  buffer.printf("lines = %d\r\n", _ydim);
  buffer.printf("columns = %d\r\n", _xdim);
  for (int line = 0; line < _ydim; ++line){
   for (int col = 0; col < _xdim; ++col){
    offset = node*_nways;
    buffer.printf("node %d = %d:%d:%d:%d:%d\r\n",
     node, _paths[offset + 0], _paths[offset + 1],
     _paths[offset + 2], _paths[offset + 3], _bonus[node++]);
   }
  }
  buffer.printf("\r\n");
  File file = new File(filename, FileMode.Out);
  file.write(buffer.toString());
  file.close();

thanks,
Miguel


June 01, 2004
I noticed that FileMode.Out overrides the file it it exists previously... the result is a file that is a mix of two files!?!

(running windows xp sp1/dmd 0.91)


June 01, 2004
>   file.write(buffer.toString());

stream.write(char[]) will first write the length of the string and then the
string contents. The function writeString will just write the contents:
  file.writeString(buffer.toString());


June 01, 2004
<shameless plug>
this is where multiple distinct readers/writers (BinaryWriter, DisplayWriter
etc)really helps within mango.io :-)
</shameless plug>

"Ben Hinkle" <bhinkle4@juno.com> wrote in message news:c9go14$28s4$1@digitaldaemon.com...
>
> >   file.write(buffer.toString());
>
> stream.write(char[]) will first write the length of the string and then
the
> string contents. The function writeString will just write the contents:
>   file.writeString(buffer.toString());
>
>


June 01, 2004
Thanks. One problem solved...
what can I do to have the writeString() function not overriding an existing
file, I mean delete the file and then write,
just like an fopen(filename, "w");


"Ben Hinkle" <bhinkle4@juno.com> escreveu na mensagem news:c9go14$28s4$1@digitaldaemon.com...
>
> >   file.write(buffer.toString());
>
> stream.write(char[]) will first write the length of the string and then
the
> string contents. The function writeString will just write the contents:
>   file.writeString(buffer.toString());
>
>


June 01, 2004
Miguel Ferreira Simes wrote:

> Thanks. One problem solved...
> what can I do to have the writeString() function not overriding an
> existing file, I mean delete the file and then write,
> just like an fopen(filename, "w");

I think you have to use std.file:
 if (std.file.exist(filename)) std.file.remove(filename);
and catch any exceptions that may be thrown if something fails.
June 01, 2004
"Ben Hinkle" <bhinkle4@juno.com> wrote in message news:c9gs25$2ei4$1@digitaldaemon.com...
> Miguel Ferreira Simes wrote:
>
> > Thanks. One problem solved...
> > what can I do to have the writeString() function not overriding an
> > existing file, I mean delete the file and then write,
> > just like an fopen(filename, "w");
>
> I think you have to use std.file:
>  if (std.file.exist(filename)) std.file.remove(filename);
> and catch any exceptions that may be thrown if something fails.

File has a create method.


June 02, 2004
On Tue, 1 Jun 2004 03:47:27 +0100
"Miguel Ferreira Simões" <kobold@netcabo.pt> wrote:

> Thanks. One problem solved...
> what can I do to have the writeString() function not overriding an
> existing file, I mean delete the file and then write,
> just like an fopen(filename, "w");

What version of dmd or gdc are you using for this?  Using dmd 0.90 and/or the current gdc, my files are overwritten (completely recreated) when using File(someString, FileMode.Out) to construct the File stream.

Dennis
June 02, 2004
I am using dmd 0.91. (windows xp sp1)

"Dennis Walters, II" <scruff@myrealbox.com> escreveu na mensagem
news:20040601232230.6a15982c.scruff@myrealbox.com...
On Tue, 1 Jun 2004 03:47:27 +0100
"Miguel Ferreira Simões" <kobold@netcabo.pt> wrote:

> Thanks. One problem solved...
> what can I do to have the writeString() function not overriding an
> existing file, I mean delete the file and then write,
> just like an fopen(filename, "w");

What version of dmd or gdc are you using for this?  Using dmd 0.90 and/or the current gdc, my files are overwritten (completely recreated) when using File(someString, FileMode.Out) to construct the File stream.

Dennis