www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - FIle I/O

reply Graham Nicholls <graham rockcons.co.uk> writes:
I'm writing a program to take a file and convert it into a binary format which
matches the format produced by a system which we use.  If I get it right, this
will allow me to "replay" the file into the system.  However I can't find how
to do I/O in D.  I've got the "D Programming Language" and "Tango" books, but
not much there, and as a C/C++ programmer, Tango doesn't seem that appealing
:-).  Is there a tutorial anywhere?
Thanks
Sep 16 2010
next sibling parent reply Tom Kazimiers <2voodoo gmx.de> writes:
Hi Graham,

On 09/16/2010 04:28 PM, Graham Nicholls wrote:
 I'm writing a program to take a file and convert it into a binary format which
 matches the format produced by a system which we use.  If I get it right, this
 will allow me to "replay" the file into the system.  However I can't find how
 to do I/O in D.  I've got the "D Programming Language" and "Tango" books, but
 not much there, and as a C/C++ programmer, Tango doesn't seem that appealing
 :-).  Is there a tutorial anywhere?
in case you did not find some example code yet: http://www.dprogramming.com/FileTutorial.html Regards, Tom
Sep 16 2010
next sibling parent Graham Nicholls <graham rockcons.co.uk> writes:
Thanks.  Not sure how I didn't find that - I'm looking now.
Graham
Sep 16 2010
prev sibling next sibling parent reply Graham Nicholls <graham rockcons.co.uk> writes:
Is this D 1.0 ? I get errors regarding printf - I understood that writeln was
the
2.0 way.
Thanks
Sep 16 2010
parent Tom Kazimiers <2voodoo gmx.de> writes:
Graham,

On 09/16/2010 05:02 PM, Graham Nicholls wrote:
 Is this D 1.0 ? I get errors regarding printf - I understood that writeln was
the
 2.0 way.
Yes, I think it's D 1.0. For a D 2.0 version I replaced those printf's with writeln's, too. Bye, Tom
Sep 16 2010
prev sibling parent reply Kagamin <spam here.lot> writes:
Tom Kazimiers Wrote:

 Hi Graham,
 
 On 09/16/2010 04:28 PM, Graham Nicholls wrote:
 I'm writing a program to take a file and convert it into a binary format which
 matches the format produced by a system which we use.  If I get it right, this
 will allow me to "replay" the file into the system.  However I can't find how
 to do I/O in D.  I've got the "D Programming Language" and "Tango" books, but
 not much there, and as a C/C++ programmer, Tango doesn't seem that appealing
 :-).  Is there a tutorial anywhere?
in case you did not find some example code yet: http://www.dprogramming.com/FileTutorial.html Regards, Tom
Weren't streams converted to ranges?
Sep 16 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, September 16, 2010 13:16:03 Kagamin wrote:
 Tom Kazimiers Wrote:
 Hi Graham,
 
 On 09/16/2010 04:28 PM, Graham Nicholls wrote:
 I'm writing a program to take a file and convert it into a binary
 format which matches the format produced by a system which we use.  If
 I get it right, this will allow me to "replay" the file into the
 system.  However I can't find how to do I/O in D.  I've got the "D
 Programming Language" and "Tango" books, but not much there, and as a
 C/C++ programmer, Tango doesn't seem that appealing
 
 :-).  Is there a tutorial anywhere?
in case you did not find some example code yet: http://www.dprogramming.com/FileTutorial.html Regards, Tom
Weren't streams converted to ranges?
Not yet. std.stream as it is is going to be deprecated/removed. However, the Phobos team does not yet have a replacement for it (which will be a range-based stream solution). I believe that there's at least one implementation floating around which might make it into Phobos, but nothing has been approved yet for inclusion. - Jonathan M Davis
Sep 16 2010
parent reply Graham Nicholls <graham rockcons.co.uk> writes:
I'm getting a little confused.  I've installed a .deb package of d 2.0, but now
my
code won't compile:
unlogcat.d(112): Error: std.stream.File at
/usr/include/d/dmd/phobos/std/stream.d(1787) conflicts with std.stdio.File at
/usr/include/d/dmd/phobos/std/stdio.d(248)

Yet if I leave either out, I get unresolved symbols.  Also, where can I find out
what exceptions can be raised for a particular operation ?
Thanks
Sep 17 2010
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 17 Sep 2010 12:21:07 -0400, Graham Nicholls  
<graham rockcons.co.uk> wrote:

 I'm getting a little confused.  I've installed a .deb package of d 2.0,  
 but now my
 code won't compile:
 unlogcat.d(112): Error: std.stream.File at
 /usr/include/d/dmd/phobos/std/stream.d(1787) conflicts with  
 std.stdio.File at
 /usr/include/d/dmd/phobos/std/stdio.d(248)

 Yet if I leave either out, I get unresolved symbols.  Also, where can I  
 find out
 what exceptions can be raised for a particular operation ?
 Thanks
Your code probably is including both stdio and stream. Then you use the simple type File, which is defined in both. Try disambiguating by using std.stdio.File or std.stream.File. Or alternatively, you can rename imports (don't remember the exact syntax). -Steve
Sep 17 2010
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, September 17, 2010 09:21:07 Graham Nicholls wrote:
 I'm getting a little confused.  I've installed a .deb package of d 2.0, but
 now my code won't compile:
 unlogcat.d(112): Error: std.stream.File at
 /usr/include/d/dmd/phobos/std/stream.d(1787) conflicts with std.stdio.File
 at /usr/include/d/dmd/phobos/std/stdio.d(248)
 
 Yet if I leave either out, I get unresolved symbols.  Also, where can I
 find out what exceptions can be raised for a particular operation ?
 Thanks
If you import two modules which have the same symbol in them (be it a function, a struct, or whatnot) and you use that symbol in your code without fully qualifying it, and it's ambiguous as to which one you meant, the compiler won't compile it. You have to fully qualify it. From the looks of it, you have imported both std.stream (which is going to be deprecated by the way) and std.stdio. std.stream has a File class and std.stdio has a File struct. So, if you use File in your module, then it's ambiguous as to which you mean. To avoid the problem you either have to a named import (or whatever it's called) - e.g. import io = std.stdio - and use that name to qualify anything you use in that module (e.g. io.File), or you have to fully qualify the name (std.stdio.File). You might be able to specifically import a symbol from a module, but I don't remember how. In any case, your problem is that you've imported two modules with the same symbol in them, and you've tried to use that symbol, so the compiler doesn't know which one you mean, and you have to be more specific so that it knows which you mean. - Jonathan M Davis
Sep 17 2010
parent reply Graham Nicholls <graham rockcons.co.uk> writes:
Thanks.  I figured that it was a duplicate definition, but not if I leave out
st.stream, then OpenException is undefined.  Is there a documentation for the
exceptions which are in the relevant packages - I'll struggle without it!

And thanks, all for the help - much appreciated.

BTW, am I making a mistake using 2.0 rather than v1.x?
It seems that perhaps 2.0 is not entirely complete - the libraries are pretty
fundamental, after all.
Thanks again.
Sep 18 2010
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday 18 September 2010 08:50:41 Graham Nicholls wrote:
 Thanks.  I figured that it was a duplicate definition, but not if I leave
 out st.stream, then OpenException is undefined.  Is there a documentation
 for the exceptions which are in the relevant packages - I'll struggle
 without it!
You'll have to look at the documentation for a particular module to see if it defines any exceptions. If it doesn't list any, then odds are that if it throws anything, it'll be Exception, but the docs may not be complete.
 And thanks, all for the help - much appreciated.
 
 BTW, am I making a mistake using 2.0 rather than v1.x?
 It seems that perhaps 2.0 is not entirely complete - the libraries are
 pretty fundamental, after all.
 Thanks again.
The language definition has stabilized (though there's still plenty of bug fixing going on). Andrei Alexandrescu has recently release a book - "The D Programming Language" - which goes over the language (highly recommended). However, Phobos, the standard library is still very much a work in progress. Much of it will stay as it is, but there's still a lot of work going on and changes being made, though they try to not break code between releasing but rather deprecate code and then remove it a release or two later. The definition of D2 only reached stability a few months ago, so the work on Phobos has picked up since then. D1 is really a different language, though D2 originates from D1. D1 is stable, including the library. But Phobos in D1 is pretty pathetic really, which is why many people use the third party library Tango (which is not compatible with Phobos since Tango replaces some core stuff like the garbage collector). Tango is very different from Phobos. Also, in using D1, you lose a lot of nice features that D2 has. Whether you use D1 or D2 depends entirely on what you're doing. A copy like Boeing who needs completely rock-solid code, because lives depend on it, should probably use D1. An individual writing code for themselves can use D2 just fine as long as they're willing to put up with the occasional bugs in the compiler and standard libraries as well as the fact that portions of the standard libraries do change from release to release. Companies doing code that lives don't depend on could use D2 as well, but how good an idea that would be would depend on what they're doing. D2 is definitely stable enough for use, but Phobos is still very much evolving and there are bugs in the compiler which pop up from time to time which can be highly annoying. So, it all depends on what you intend to get out of D2 and how much hassle you're willing to put up with. Personally, I think that the occasional issue is well worth the power and flexibility of D2, but you may not find that to be true for you. The compiler continues to get bugs fixed, and Phobos is having lots of work done on it, so we are approaching the point where it will be obvious that D2 is the one to pick rather than D1, but it still depends at this point. I wrote a longer response to a similar question on stack overflow ( http://stackoverflow.com/questions/3205509/what-are-the-current-challenges-of- - Jonathan M Davis
Sep 18 2010
prev sibling parent "Nick Sabalausky" <a a.a> writes:
"Graham Nicholls" <graham rockcons.co.uk> wrote in message 
news:i6t9ig$1ffs$1 digitalmars.com...
 I'm writing a program to take a file and convert it into a binary format 
 which
 matches the format produced by a system which we use.  If I get it right, 
 this
 will allow me to "replay" the file into the system.  However I can't find 
 how
 to do I/O in D.  I've got the "D Programming Language" and "Tango" books, 
 but
 not much there, and as a C/C++ programmer, Tango doesn't seem that 
 appealing
 :-).  Is there a tutorial anywhere?
 Thanks
File/path I/O is kind of a pain in Tango. You might want to consider D2/Phobos for that: http://www.digitalmars.com/d/2.0/phobos/std_file.html If you want to use D1/Tango though, the API reference is here: http://www.dsource.org/projects/tango/docs/stable
Sep 16 2010