Thread overview
Opening a file for writing
Jul 05, 2005
jicman
Jul 05, 2005
Regan Heath
Jul 05, 2005
jicman
Jul 05, 2005
Dejan Lekic
Jul 05, 2005
jicman
July 05, 2005
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é


July 05, 2005
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
July 05, 2005
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

July 05, 2005
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é


July 05, 2005
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
>