FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Working with files

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
dhasenan



Joined: 03 Feb 2005
Posts: 73
Location: New York

PostPosted: Fri Feb 04, 2005 12:35 pm    Post subject: Working with files Reply with quote

Two questions--

First, how do I go about creating a file through D? And how do I write to that file? Streams don't cut it, apparently; I keep getting errors when I try that. (And it was a five-line program, so yes, that was the error. The specific text was "Error: unable to write to stream".)

Here's the little test program I created:

Code:
import std.stream;
void main() {
   File f = new File("PATH/blargh");
   f.writeLine("Bleagh!");
   f.close();
}


Any suggestions?
Back to top
View user's profile Send private message AIM Address
SpottedTiger



Joined: 29 Nov 2004
Posts: 7
Location: Pennsylvania, USA

PostPosted: Fri Feb 04, 2005 10:06 pm    Post subject: Reply with quote

dhasenan, by default the following "File f = new File( r"C:\blargh.txt" )" line does an open and not a create as you wanted. Please look at the D code below that I've tested, which seems to do what you want.

Code:

private import std.stream;
private import std.stdio;

void main()
{
   // FileMode.In     - Read In
   // FileMode.Out    - Write To
   // FileMode.OutNew - Create \ Empty Existing
   // FileMode.Append - Write Appending Data
   File f = new File( r"C:\blargh.txt", FileMode.OutNew );
   char[] sLine1;
   char[] sLine2;
   
   // Creates a file or
   // Empty the exists file
   //f.create( r"C:\blargh.txt" ); // same as the .OutNew above
   f.writeLine( "Bleagh!" );
   f.close();
   
   // Opens and read from the file
   f.open( r"C:\blargh.txt", FileMode.In );
   sLine1 = f.readLine();
   f.close();
   
   writefln( "1st.sLine1=\"?s\"", sLine1 );
   
   // Append new data to the end of the file
   f.open( r"C:\blargh.txt", FileMode.Append );
   f.writeLine( "Bleagh2!" );
   f.close();
 
   f.open( r"C:\blargh.txt", FileMode.In );
   sLine1 = f.readLine();
   sLine2 = f.readLine();
   f.close();
   
   writefln( "2nd.sLine1=\"?s\"", sLine1 );
   writefln( "2nd.sLine2=\"?s\"", sLine2 );
 
   // Deletes the file
   f.remove( r"C:\blargh.txt" );
}


Quote:
Output:
C:\dmd>bin\dmd streamtest.d
C:\dmd\bin\..\..\dm\bin\link.exe streamtest,,,user32+kernel32/noi;

C:\dmd>streamtest
1st.sLine1="Bleagh!"
2nd.sLine1="Bleagh!"
2nd.sLine2="Bleagh2!"

C:\dmd>


David L.
_________________
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Back to top
View user's profile Send private message Send e-mail
dhasenan



Joined: 03 Feb 2005
Posts: 73
Location: New York

PostPosted: Sat Feb 05, 2005 1:41 am    Post subject: Reply with quote

Thanks. Dare I ask, though, where this is documented?
Back to top
View user's profile Send private message AIM Address
SpottedTiger



Joined: 29 Nov 2004
Posts: 7
Location: Pennsylvania, USA

PostPosted: Sat Feb 05, 2005 5:28 pm    Post subject: Reply with quote

dhasenan: You're welcome! Very Happy

Well, for a high level view that gives all the functions and their parameters, you'll have to go to the DigitalMars D website (http://www.digitalmars.com/d/) then click on the "Phobos (Runtime Library)" link, and from there click on the "std.stream" link or scroll down to it. But to really find out more details as I had to do in answering your question, you'll have to look into the stream.d file itself...which I found at "C:\dmd\src\phobos\std\stream.d" on my WinXP machine.

Also looking at the "Tutorials" section here at dSource.org (http://www.dsource.org/tutorials/) will many times have the an example, or even my site's small "D Programming 101" section (http://spottedtiger.tripod.com/D_Language/D_Hub_D_Basics_XP.html), but neither one currently has a good example that fits your question. So, I guess I should go ahead and add this example I wrote for you and put it in the tutorials section to help others.

Anyway, I hope you'll continue to explore programming in D, and if you have a question just feel free to ask. There are many very friendly and helpful people here. Wink
_________________
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Back to top
View user's profile Send private message Send e-mail
jcc7



Joined: 22 Feb 2004
Posts: 657
Location: Muskogee, OK, USA

PostPosted: Sat Feb 05, 2005 9:47 pm    Post subject: Reply with quote

SpottedTiger wrote:
So, I guess I should go ahead and add this example I wrote for you and put it in the tutorials section to help others.
Thanks for adding the example.

I would've sworn there was already a simple example for output to a file using std.stream, but I can't find it now so I must've imagined it. Wink
Back to top
View user's profile Send private message AIM Address
SpottedTiger



Joined: 29 Nov 2004
Posts: 7
Location: Pennsylvania, USA

PostPosted: Sun Feb 06, 2005 11:57 pm    Post subject: Reply with quote

jcc7: You're welcome...yeah I thought there was one too. But all I found was your example reading in a input file.
_________________
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group