www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - writing files: trouble

reply "Miguel Ferreira Simões" <kobold netcabo.pt> writes:
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
May 31 2004
next sibling parent "Miguel Ferreira Simões" <kobold netcabo.pt> writes:
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)
May 31 2004
prev sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
   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());
May 31 2004
next sibling parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
<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());
May 31 2004
prev sibling parent reply "Miguel Ferreira Simões" <kobold netcabo.pt> writes:
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());
May 31 2004
next sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
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.
May 31 2004
parent "Vathix" <vathixSpamFix dprogramming.com> writes:
"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.
May 31 2004
prev sibling parent reply "Dennis Walters, II" <scruff myrealbox.com> writes:
On Tue, 1 Jun 2004 03:47:27 +0100
"Miguel Ferreira Sim=F5es" <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
Jun 01 2004
parent "Miguel Ferreira Simões" <kobold netcabo.pt> writes:
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
Jun 02 2004