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
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
parent reply Seth Hoenig <seth.a.hoenig gmail.com> writes:
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.
Oct 06 2010
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
next sibling 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