View mode: basic / threaded / horizontal-split · Log in · Help
June 01, 2004
writing files: trouble
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
Re: writing files: trouble
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
Re: writing files: trouble
>   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
Re: writing files: trouble
<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
Re: writing files: trouble
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
Re: writing files: trouble
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
Re: writing files: trouble
"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
Re: writing files: trouble
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
Re: writing files: trouble
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
Top | Discussion index | About this forum | D home