Thread overview
how to access a file?
Aug 26, 2002
Andrew Edwards
Aug 26, 2002
Pavel Minayev
Aug 27, 2002
Edmund Huber
Aug 28, 2002
Pavel Minayev
Jan 12, 2015
Laeeth Isharc
August 26, 2002
Sorry for the trouble, but could someone demonstrate of how to accomplish the following tasks in D?

Read a file
Write a file
Append to a file
Rename a file
Delete a file
Get file size
Get file attributes

Thanks in advance...
Andrew

August 26, 2002
On Mon, 26 Aug 2002 02:49:51 -0400 "Andrew Edwards" <crxace13@comcast.net> wrote:

> Sorry for the trouble, but could someone demonstrate of how to accomplish the following tasks in D?
> 
> Read a file

	File file = new File('c:\autoexec.bat');
	while (!file.eof())
	{
		char[] s = file.readLine();
		printf("%.*s\n", s);
	}

> Write a file

	File file = new File;
	file.create('fib.dat');
	int a = 0, b = 1;
	for (int i = 0; i < 1000; i++)
	{
		int c = a + b;
		file.write(c);
		a = b; b = c;
	}

> Append to a file

	File file = new File('fib.dat', FileMode.In | FileMode.Out);
	file.seek(0, SeekPos.End);
	file.writeString("END OF FILE");

> Rename a file

	rename("old", "new");

> Delete a file

	remove("kernel32.dll");

> Get file size

	printf("Size of autoexec.bat is %d bytes\n", (new
File("c:\autoexec.bat")).size());

> Get file attributes

Not sure how this could be done - it's not multiplatform.
August 27, 2002
If you must get the file attributes, you could use the winapi (for Windows only
of course):
DWORD GetFileAttributes( LPCTSTR lpFileName );

Detailed here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/getfileattributes.asp

You also need to do this:
import windows;
And remember that D's strings don't end with the NULL character (I think?); you
have to append it yourself before you send it off. Anyway, I'm fairly new at D,
so if anyone can clarify I'd be grateful.

Pavel Minayev wrote:

> On Mon, 26 Aug 2002 02:49:51 -0400 "Andrew Edwards" <crxace13@comcast.net> wrote:
>
> > Sorry for the trouble, but could someone demonstrate of how to accomplish the following tasks in D?
> >
> > Read a file
>
>         File file = new File('c:\autoexec.bat');
>         while (!file.eof())
>         {
>                 char[] s = file.readLine();
>                 printf("%.*s\n", s);
>         }
>
> > Write a file
>
>         File file = new File;
>         file.create('fib.dat');
>         int a = 0, b = 1;
>         for (int i = 0; i < 1000; i++)
>         {
>                 int c = a + b;
>                 file.write(c);
>                 a = b; b = c;
>         }
>
> > Append to a file
>
>         File file = new File('fib.dat', FileMode.In | FileMode.Out);
>         file.seek(0, SeekPos.End);
>         file.writeString("END OF FILE");
>
> > Rename a file
>
>         rename("old", "new");
>
> > Delete a file
>
>         remove("kernel32.dll");
>
> > Get file size
>
>         printf("Size of autoexec.bat is %d bytes\n", (new
> File("c:\autoexec.bat")).size());
>
> > Get file attributes
>
> Not sure how this could be done - it's not multiplatform.

August 28, 2002
On Tue, 27 Aug 2002 19:33:54 -0400 Edmund Huber <weql@hotmail.com> wrote:

> And remember that D's strings don't end with the NULL character (I think?);
you
> have to append it yourself before you send it off. Anyway, I'm fairly new at
D,
> so if anyone can clarify I'd be grateful.

There is a toStringz() function in string module which can be used to convert
D strings
to C ones. It performes some checks before converting, and in some cases it can
be a lot faster than just appending null to the end.
January 12, 2015
On Tuesday, 27 August 2002 at 23:31:35 UTC, Edmund Huber wrote:
> If you must get the file attributes, you could use the winapi (for Windows only
> of course):
> DWORD GetFileAttributes( LPCTSTR lpFileName );
>
> Detailed here:
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/getfileattributes.asp
>
> You also need to do this:
> import windows;

What's the best way to do this today?  (Replying to a message from 13 years ago!)

I am writing an installer for mini windows client and want to know where I have permission to save the executable files.  (Administered machine, so user won't have full rights).

The msdn link above is dead by now.