www.digitalmars.com         C & C++   DMDScript  

D - how to access a file?

reply "Andrew Edwards" <crxace13 comcast.net> writes:
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
Aug 25 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
On Mon=2C 26 Aug 2002 02=3A49=3A51 -0400 =22Andrew Edwards=22
=3Ccrxace13=40comcast=2Enet=3E 
wrote=3A

=3E Sorry for the trouble=2C but could someone demonstrate of how to accomplish
=3E the following tasks in D=3F
=3E 
=3E Read a file

=09File file =3D new File=28'c=3A=5Cautoexec=2Ebat'=29=3B
=09while =28!file=2Eeof=28=29=29
=09{
=09=09char=5B=5D s =3D file=2EreadLine=28=29=3B
=09=09printf=28=22%=2E*s=5Cn=22=2C s=29=3B
=09}

=3E Write a file

=09File file =3D new File=3B
=09file=2Ecreate=28'fib=2Edat'=29=3B
=09int a =3D 0=2C b =3D 1=3B
=09for =28int i =3D 0=3B i =3C 1000=3B i++=29
=09{
=09=09int c =3D a + b=3B
=09=09file=2Ewrite=28c=29=3B
=09=09a =3D b=3B b =3D c=3B
=09}

=3E Append to a file

=09File file =3D new File=28'fib=2Edat'=2C FileMode=2EIn | FileMode=2EOut=29=3B
=09file=2Eseek=280=2C SeekPos=2EEnd=29=3B
=09file=2EwriteString=28=22END OF FILE=22=29=3B

=3E Rename a file

=09rename=28=22old=22=2C =22new=22=29=3B

=3E Delete a file

=09remove=28=22kernel32=2Edll=22=29=3B

=3E Get file size

=09printf=28=22Size of autoexec=2Ebat is %d bytes=5Cn=22=2C =28new 
File=28=22c=3A=5Cautoexec=2Ebat=22=29=29=2Esize=28=29=29=3B

=3E Get file attributes

Not sure how this could be done - it's not multiplatform=2E
Aug 26 2002
parent reply Edmund Huber <weql hotmail.com> writes:
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.
Aug 27 2002
next sibling parent Pavel Minayev <evilone omen.ru> writes:
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.
Aug 27 2002
prev sibling parent "Laeeth Isharc" <laeethnospam spammenot_laeeth.com> writes:
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.
Jan 12 2015