Binary I/O for Newbie

2012-02-27 Thread tjb
All, I am just starting to learn D. I am an economist - not a programmer, so I appreciate your patience with lack of knowledge. I have some financial data in a binary file that I would like to process. In C++ I have the data in a structure like this: struct TaqIdx { char symbol[10];

Re: Binary I/O for Newbie

2012-02-27 Thread Robert Jacques
On Mon, 27 Feb 2012 12:21:21 -0600, tjb broug...@gmail.com wrote: All, I am just starting to learn D. I am an economist - not a programmer, so I appreciate your patience with lack of knowledge. I have some financial data in a binary file that I would like to process. In C++ I have the

Re: Binary I/O for Newbie

2012-02-27 Thread Justin Whear
On Mon, 27 Feb 2012 19:21:21 +0100, tjb wrote: All, I am just starting to learn D. I am an economist - not a programmer, so I appreciate your patience with lack of knowledge. I have some financial data in a binary file that I would like to process. In C++ I have the data in a structure

Re: Binary I/O for Newbie

2012-02-27 Thread Ali Çehreli
On 02/27/2012 10:21 AM, tjb wrote: I have some financial data in a binary file that I would like to process. In C++ I have the data in a structure like this: struct TaqIdx { char symbol[10]; int tdate; int begrec; int endrec; } The equivalent of that C++ (and C) struct would be almost

Re: Binary I/O for Newbie

2012-02-27 Thread Tobias Brandt
Doesn't the struct alignment play a role here?

Re: Binary I/O for Newbie

2012-02-27 Thread Justin Whear
On Mon, 27 Feb 2012 19:42:36 +0100, Tobias Brandt wrote: Doesn't the struct alignment play a role here? Good point. If the data is packed, you can toss an align(1) on the front of the struct declaration.

Re: Binary I/O for Newbie

2012-02-27 Thread tjb
On Monday, 27 February 2012 at 18:56:15 UTC, Justin Whear wrote: On Mon, 27 Feb 2012 19:42:36 +0100, Tobias Brandt wrote: Doesn't the struct alignment play a role here? Good point. If the data is packed, you can toss an align(1) on the front of the struct declaration. So, something like

Re: Binary I/O for Newbie

2012-02-27 Thread Tobias Brandt
So, something like this should work: [...] It really depends on how you wrote the file originally. If you know that it is packed, i.e. 10+32+32+32=106 bytes per record, then yes. If you wrote to the file with a C++ program, then I guess the compiler aligned the data so that the whole struct is

Re: Binary I/O for Newbie

2012-02-27 Thread Ali Çehreli
On 02/27/2012 11:27 AM, Tobias Brandt wrote: So, something like this should work: [...] It really depends on how you wrote the file originally. If you know that it is packed, i.e. 10+32+32+32=106 bytes per record, then yes. You meant 4 bytes per int. :) If you wrote to the file with a

Re: Binary I/O for Newbie

2012-02-27 Thread Tobias Brandt
It really depends on how you wrote the file originally. If you know that it is packed, i.e. 10+32+32+32=106 bytes per record, then yes. You meant 4 bytes per int. :) Yep, good catch. If you wrote to the file with a C++ program, then I guess the compiler aligned the data so that the whole

Re: Binary I/O for Newbie

2012-02-27 Thread Ali Çehreli
On 02/27/2012 11:43 AM, Tobias Brandt wrote: If you wrote to the file with a C++ program, then I guess the compiler aligned the data so that the whole struct is 128 bytes in size. Technically, the C++ compiler is allowed to do anything short of changing the order of the struct fields. That

Re: Binary I/O for Newbie

2012-02-27 Thread tjb
On Monday, 27 February 2012 at 19:28:07 UTC, Tobias Brandt wrote: So, something like this should work: [...] It really depends on how you wrote the file originally. If you know that it is packed, i.e. 10+32+32+32=106 bytes per record, then yes. If you wrote to the file with a C++ program,

Re: Binary I/O for Newbie

2012-02-27 Thread Tobias Brandt
Just looked at my old C++ code.  And the struct looks like this: struct TaqIdx {  char symbol[10];  int tdate;  int begrec;  int endrec; }__attribute__((packed)); So I am guessing I want to use the align(1) as Justin suggested. Correct? Yes.