www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - write to file ... trivial?

reply Dr. Smith <iam far.out> writes:
This should be trivial.  However, I've not found in the documentation (trying
both std.stdio and std.file) how to write to a file in the manner here:

filename.writefln("%s\t%f", someString, someDouble);

... this merely prints filename to screen ... does not create a data file.
Oct 06 2010
next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Wed, 06 Oct 2010 22:43:42 +0400, Dr. Smith <iam far.out> wrote:

 This should be trivial.  However, I've not found in the documentation  
 (trying
 both std.stdio and std.file) how to write to a file in the manner here:

 filename.writefln("%s\t%f", someString, someDouble);

 ... this merely prints filename to screen ... does not create a data  
 file.

In your case, foo.bar(args) == bar(foo, args) - that is call Uniform Function Call Syntax, that is: filename.writefln("%s\t%f", someString, someDouble); -> writefln(filename, "%s\t%f", someString, someDouble); which in turn tries using filename as a format. Try using std.stream. It should be something like this: File file = new File(fileName, FileMode.OutNew); // open file for writing, create new if none exists file.writefln("hello, %s!", "world"); Not tested but should work.
Oct 06 2010
next sibling parent reply Dr. Smith <iam far.out> writes:
Thank you.  Indeed, I forgot: auto f = File("outfile.txt", "w");

Interestingly, this apparently works within a for-loop to overwrite the file on
the first iteration and appending otherwise (Should there not be an explicit
append arg?):

for(int i = 0; i < 100; i++) {
  f.writefln("%s%i", "World Hello", i);
} f.close();                 // f.close outside the loop for efficiency?

If someone could point me to the online documentation for this matter, I'd
appreciate it.  I'd prefer to use this forum for less elementary matters.
Oct 06 2010
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Dr. Smith Wrote:

 Thank you.  Indeed, I forgot: auto f = File("outfile.txt", "w");
 
 Interestingly, this apparently works within a for-loop to overwrite the file on
 the first iteration and appending otherwise (Should there not be an explicit
 append arg?):
 
 for(int i = 0; i < 100; i++) {
   f.writefln("%s%i", "World Hello", i);
 } f.close();                 // f.close outside the loop for efficiency?
 
 If someone could point me to the online documentation for this matter, I'd
 appreciate it.  I'd prefer to use this forum for less elementary matters.

auto f = File("name", "options"); Creates a new File object which, based on the options, opens the file for writing (create and replace), reading (binary or text), or appending. All operations are performed on this open file until closed and reopened. So for your example if you do not wish to append to an open file you can do this Now you can get the same behavior even if you close the File with: for(int i = 0; i < 100; i++) { auto f = File("name.txt", "a"); f.writefln("%s%i", "World Hello", i); f.close(); } Notice the "a" instead of "w" This is appending to the file when it opens it. Thus the reason the first example has f.close outside the loop is because it is opening the File outside the loop. http://digitalmars.com/d/2.0/phobos/std_file.html
Oct 06 2010
prev sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 07 Oct 2010 01:36:23 +0400, Dr. Smith <iam far.out> wrote:

 Thank you.  Indeed, I forgot: auto f = File("outfile.txt", "w");

 Interestingly, this apparently works within a for-loop to overwrite the  
 file on
 the first iteration and appending otherwise (Should there not be an  
 explicit
 append arg?):

That's a correct behavior. See how it works: when you open a file, you a get a "pointer" into that file. Every time you write N bytes to that file, your pointer advances the same amount. E.g. file.write("aaa"); file.write("bbb"); is essentially the same as: file.write("aaabbb"); If that is not that you want, then you need to store you pointer before writing, and seek back afterwards: (I don't know the proper syntax so that is a pseudo-code) auto pos = file.tell(); file.write("aaa"); file.seek(pos); file.write("bbb"); In this case, you will first write "aaa" to file and then overwrite it with "bbb". That's the way file I/O works in every OS I know of and that's probably because that's the way hard disks work in general. Hope that helps.
Oct 07 2010
prev sibling parent Seth Hoenig <seth.a.hoenig gmail.com> writes:
--00221532cf4455e71f0491f7b106
Content-Type: text/plain; charset=ISO-8859-1

Here's a minimal little template (checked):

import std.stdio;

void main()
{
        auto f = File("outfile.txt", "w");
        f.writefln("%s World", "Hello");
        f.close();
}





2010/10/6 Denis Koroskin <2korden gmail.com>

 On Wed, 06 Oct 2010 22:43:42 +0400, Dr. Smith <iam far.out> wrote:

  This should be trivial.  However, I've not found in the documentation
 (trying
 both std.stdio and std.file) how to write to a file in the manner here:

 filename.writefln("%s\t%f", someString, someDouble);

 ... this merely prints filename to screen ... does not create a data file.

In your case, foo.bar(args) == bar(foo, args) - that is call Uniform Function Call Syntax, that is: filename.writefln("%s\t%f", someString, someDouble); -> writefln(filename, "%s\t%f", someString, someDouble); which in turn tries using filename as a format. Try using std.stream. It should be something like this: File file = new File(fileName, FileMode.OutNew); // open file for writing, create new if none exists file.writefln("hello, %s!", "world"); Not tested but should work.

--00221532cf4455e71f0491f7b106 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Here&#39;s a minimal little template (checked):<br><br>import std.stdio;<br=
<br>void main()<br>{<br>=A0=A0=A0=A0=A0=A0=A0 auto f =3D File(&quot;outfil=

orld&quot;, &quot;Hello&quot;);<br> =A0=A0=A0=A0=A0=A0=A0 f.close();<br>}<br><br><br><br><br><br><div class=3D"= gmail_quote">2010/10/6 Denis Koroskin <span dir=3D"ltr">&lt;<a href=3D"mail= to:2korden gmail.com">2korden gmail.com</a>&gt;</span><br><blockquote class= =3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid= rgb(204, 204, 204); padding-left: 1ex;"> <div><div></div><div class=3D"h5">On Wed, 06 Oct 2010 22:43:42 +0400, Dr. S= mith &lt;iam far.out&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> This should be trivial. =A0However, I&#39;ve not found in the documentation= (trying<br> both std.stdio and std.file) how to write to a file in the manner here:<br> <br> filename.writefln(&quot;%s\t%f&quot;, someString, someDouble);<br> <br> ... this merely prints filename to screen ... does not create a data file.<= br> </blockquote> <br></div></div> In your case, foo.bar(args) =3D=3D bar(foo, args) - that is call Uniform Fu= nction Call Syntax, that is:<br> <br> filename.writefln(&quot;%s\t%f&quot;, someString, someDouble); -&gt; writef= ln(filename, &quot;%s\t%f&quot;, someString, someDouble);<br> <br> which in turn tries using filename as a format.<br> <br> Try using std.stream. It should be something like this:<br> <br> File file =3D new File(fileName, FileMode.OutNew); // open file for writing= , create new if none exists<br> file.writefln(&quot;hello, %s!&quot;, &quot;world&quot;);<br> <br> Not tested but should work.<br> </blockquote></div><br> --00221532cf4455e71f0491f7b106--
Oct 06 2010