www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Opening a file for writing

reply jicman <jicman_member pathlink.com> writes:
Greetings!

So, what is wrong with this code?

//start of code
import std.stream;
char[] fn = "C:\\logs\\DAERpt-20040212000000-20040213000000-0000.htm";
File fs = new File(fn);
fs.writeString("test");
fs.close();
//end of code

I can't compite this code with .127.  Any ideas?  I get,

jicman 17:28:28-> build FileTest.d
FileTest.d(4): found 'test' when expecting ')'
FileTest.d(4): no identifier for declarator fs.writeString
FileTest.d(4): semicolon expected, not ')'
FileTest.d(4): Declaration expected, not ')'
FileTest.d(5): no identifier for declarator fs.close

I know it's gotta be something simple. That's always my problem, but I can't see
it.  Any ideas?

thanks,

josé
Jul 05 2005
next sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Tue, 5 Jul 2005 21:48:00 +0000 (UTC), jicman  
<jicman_member pathlink.com> wrote:
 Greetings!

 So, what is wrong with this code?

 //start of code
 import std.stream;
 char[] fn = "C:\\logs\\DAERpt-20040212000000-20040213000000-0000.htm";
 File fs = new File(fn);
 fs.writeString("test");
 fs.close();
 //end of code
Is that it? Where is 'main'? Regan
Jul 05 2005
parent jicman <jicman_member pathlink.com> writes:
Regan Heath says...
On Tue, 5 Jul 2005 21:48:00 +0000 (UTC), jicman  
<jicman_member pathlink.com> wrote:
 Greetings!

 So, what is wrong with this code?

 //start of code
 import std.stream;
 char[] fn = "C:\\logs\\DAERpt-20040212000000-20040213000000-0000.htm";
 File fs = new File(fn);
 fs.writeString("test");
 fs.close();
 //end of code
Is that it? Where is 'main'?
Sorry, my mistake. Working with too many languages and interpreters at the same time. Ok, you got me. Which brings me to the problem which started this post. :-) So, if I take this new code, import std.stream; void main() { char[] fn = "C:\\DAERpt-20040212000000-20040213000000-0000.htm"; File fs = new File(fn); fs.writeString("test"); fs.close(); } and compile it and run it, I get Error: Cannot open or create file 'C:\DAERpt-20040212000000-20040213000000-0000.htm' So, the question is, how do I create a new file to write? As I said before, we should have some examples on the Phobos documentation. ;-) Yes, I know. Anyway, here is what the documentation says, this(char[] filename, FileMode mode = FileMode.In) Create the stream with no open file, an open file in read mode, or an open file with explicit file mode. mode, if given, is a combination of FileMode.In (indicating a file that can be read) and FileMode.Out (indicating a file that can be written). Opening a file for reading that doesn't exist will error. Opening a file for writing that doesn't exist will create the file. The FileMode.OutNew mode will open the file for writing and reset the legnth to zero. The FileMode.Append mode will open the file for writing and move the file position to the end of the file. How do I tell what Mode it is? Confused. Gracias (thanks) josé
Jul 05 2005
prev sibling parent reply Dejan Lekic <leka entropy.tmok.com> writes:
Jos, it does not work because File is allways opened by default as READ-ONLY
(FileMode.In).

Here is how it should be written:
/*
  file:   jicman01.d
  build:  dmd jicman01.d
  run:    ./jicman01
  Author: Dejan Lekic , http://dejan.lekic.org
*/

import std.stream;

int main(char[][] arg) 
{
  char[] fn = "test.htm";

  File fs = new File(fn, FileMode.Out);
  fs.writeString("test");
  fs.close();

  return 0;
}

Regards

...........
Dejan Lekic
  http://dejan.lekic.org
  
Jul 05 2005
parent jicman <jicman_member pathlink.com> writes:
Thanks.

Dejan Lekic says...
Jos, it does not work because File is allways opened by default as READ-ONLY
(FileMode.In).

Here is how it should be written:
/*
  file:   jicman01.d
  build:  dmd jicman01.d
  run:    ./jicman01
  Author: Dejan Lekic , http://dejan.lekic.org
*/

import std.stream;

int main(char[][] arg) 
{
  char[] fn = "test.htm";

  File fs = new File(fn, FileMode.Out);
  fs.writeString("test");
  fs.close();

  return 0;
}

Regards

...........
Dejan Lekic
  http://dejan.lekic.org
  
Jul 05 2005