www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading and writing a class to a (binary) file

reply "Omid" <mehdioa gmail.com> writes:
Hi. As I am learning D as my first language, it may sound crazy. 
But I would appreciate if somebody tell me how to read and write 
a class (say with two objects, an integer an a char[20]) to a 
file (text or binary).
Jan 07 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Omid:

 As I am learning D as my first language, it may sound crazy.
D is one of the most complex languages, so your have to learn a LOT. Learning about static typing is important, but usually as first language I suggest Python, despite Python is not perfect.
 But I would appreciate if somebody tell me how to read and 
 write a class (say with two objects, an integer an a char[20]) 
 to a file (text or binary).
This uses a text file. There are various ways to improve this code. import std.string: format; import std.stdio: File, writeln; import std.conv: to; final class Foo { private immutable int x; private immutable char[20] txt; this(in int x_, in string txt_) pure nothrow { this.x = x_; this.txt[0 .. txt_.length] = txt_; this.txt[txt_.length .. $] = ' '; } override string toString() const { return format("%d %s", x, txt); } void toFile(File fout) const { fout.writef("%x %s\n", x, txt); } static Foo fromFile(File fin) { int xx; string s; fin.readf("%x %s\n", &xx, &s); return new Foo(xx, s); } } void main() { immutable fileName = "foo_test.txt"; auto fin = File(fileName, "wt"); (new Foo(10, "First test")).toFile(fin); (new Foo(20, "Second test")).toFile(fin); fin.close(); auto fout = File(fileName, "rt"); writeln(Foo.fromFile(fout)); writeln(Foo.fromFile(fout)); fout.close(); } Bye, bearophile
Jan 07 2013
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-01-07 15:42, Omid wrote:
 Hi. As I am learning D as my first language, it may sound crazy. But I
 would appreciate if somebody tell me how to read and write a class (say
 with two objects, an integer an a char[20]) to a file (text or binary).
You can serialize it using Orange: https://github.com/jacob-carlborg/orange -- /Jacob Carlborg
Jan 07 2013
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/07/2013 06:42 AM, Omid wrote:
 Hi. As I am learning D as my first language, it may sound crazy.
I really don't think it is crazy at all. Compared especially to C and C++ I think D is extremely easy to learn. I have written a book that attempts to teach programming as a first language: http://ddili.org/ders/d.en/ The concept of a file appears in the following chapter: http://ddili.org/ders/d.en/files.html You may find the book's progress slower than you may expect, e.g. classes appear much later than files. As bearophile said though, there are many other languages like Python that may be easier to learn. I also agree with bearophile in general that there are a lot of things to learn in software. Ali
Jan 07 2013
next sibling parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Monday, 7 January 2013 at 18:59:25 UTC, Ali Çehreli wrote:
 I really don't think it is crazy at all. Compared especially to 
 C and C++ I think D is extremely easy to learn. I have written 
 a book that attempts to teach programming as a first language:
It could have been easier to learn, but certain problems come to mind. The stream library was originally (at least how I understand) an example class to show the features of c++. using << and >> sorta make sense, but when you know they mean a binary operator it makes it confusing; So a re-write before it became official could have helped a bit. The STL should have been designed differently, I've barely touched it and reading through a book on it I know for sure I hate it; Doesn't help templates in C++ are hard to use (ugly as well as semi-broken) to begin with. Hmmm if they had done a few things differently it could be very different from what it is today. 1) Fat pointer(s) like D for arrays, but allowing compatibility with raw pointers. 2) Dropped macros. Macros may be nice, but with good inline-ing and other details it's kinda pointless, and hides bugs. Less buggy template system would have removed the need entirely. 3) Iterators not been based on arrays and likely gone more with a range design. Those three alone could have made a huge difference; But instead they went the route of full backwards compatibility. I kinda think of C++ as a wort covered grandma on crack... Workable but I honestly don't want to touch it :P And who would want to with her waving her dangerous knife (unchecked, length lacking pointers) and all extensions to her house duct-taped on?
Jan 07 2013
parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Monday, 7 January 2013 at 21:28:20 UTC, Era Scarecrow wrote:
 3) Iterators not been based on arrays and likely gone more with 
 a range design.
s/arrays/pointers/
Jan 07 2013
parent reply "Omid" <mehdioa gmail.com> writes:
Thanks all, specially bearophile. I said "as my first language" 
but I didn't mean that I don't know anything about programming. I 
passed pascal around 18 years ago in my undergrad but never did 
programming since then. I should say, learning D is more fun and 
logical for me, compared to my several attempts to learn C++.
Jan 07 2013
parent Matthew Caron <matt.caron redlion.net> writes:
On 01/07/2013 05:00 PM, Omid wrote:
 Thanks all, specially bearophile. I said "as my first language" but I
 didn't mean that I don't know anything about programming. I passed
 pascal around 18 years ago in my undergrad but never did programming
 since then. I should say, learning D is more fun and logical for me,
 compared to my several attempts to learn C++.
My elevator pitch to folks is: "D is C++ done correctly" It really is a joy. -- Matthew Caron, Software Build Engineer Sixnet, a Red Lion business | www.sixnet.com +1 (518) 877-5173 x138 office
Jan 07 2013
prev sibling parent "Omid" <mehdioa gmail.com> writes:
On Monday, 7 January 2013 at 18:59:25 UTC, Ali Çehreli wrote:
 On 01/07/2013 06:42 AM, Omid wrote:
 Hi. As I am learning D as my first language, it may sound
crazy. I really don't think it is crazy at all. Compared especially to C and C++ I think D is extremely easy to learn. I have written a book that attempts to teach programming as a first language: http://ddili.org/ders/d.en/ The concept of a file appears in the following chapter: http://ddili.org/ders/d.en/files.html You may find the book's progress slower than you may expect, e.g. classes appear much later than files. As bearophile said though, there are many other languages like Python that may be easier to learn. I also agree with bearophile in general that there are a lot of things to learn in software. Ali
Thanks Ali. Very useful. Keep the nice job going.
Jan 07 2013