You're welcome.

by Michael Calkins (Login MCalkins)
C-Forum

 
>I dont quite see what you did to make the struct normal (as you say) so it uses . instead of ->

I changed:

SalesRecord* sRec;
sRec = new SalesRecord();

to:

SalesRecord sRec;

>I know you said file I/O is not your thing

File I/O in C++, because of the "streams". I can do file I/O in QBASIC well enough.

>They are allways 0 in the program

They are all 0 except the first one. The first input reads the whole thing, and there is nothing left for the others.

>This version has as you suggested, binary format for the file

Okay. I don't see the change. Again, this whole OOP concept of streams is strange to me.

>i managed to fix the records size problem i was having to.

It seems to be correctly figuring size of file in bytes / 20. (20 = 5 ints of 4 bytes each. Since they are all the same size, there is no padding.) However, the contents of the file are still ASCII decimal numbers, instead of raw binary data. So, the file varies in length, and it is a coincidence that it matches in this case.

If you insert the test pattern:

sRec.Item1 = 55;
sRec.Item2 = 0;
sRec.Item3 = 1;
sRec.Item4 = 2;
sRec.Item5 = 0;

You'll see that you get a 720 byte file: (I inserted line breaks to keep it from screwing up the post.)

55012055012055012055012055012055012055012055012055012055012055012055012055012055
01205501205501205501205501205501205501205501205501205501205501205501205501205501
20550120550120550120550120550120550120550120550120550120550120550120550120550120
55012055012055012055012055012055012055012055012055012055012055012055012055012055
01205501205501205501205501205501205501205501205501205501205501205501205501205501
20550120550120550120550120550120550120550120550120550120550120550120550120550120
55012055012055012055012055012055012055012055012055012055012055012055012055012055
01205501205501205501205501205501205501205501205501205501205501205501205501205501
20550120550120550120550120550120550120550120550120550120550120550120550120550120



This would be so much easier in QBASIC... :-)

What exactly is the file stream with the bit shift operator << doing? Is it converting the numbers to decimal strings and then concatenating them?

Is there some sort of separator that you can use that the file input will respect? I had tried comma and endl earlier without success.

Hopefully someone with C++ file I/O experience will come along and clear this up. The two solutions I'm thinking of probably wouldn't be ideal. One would be to revert to procedural file I/O, either through the C standard library, or through the operating system API. The other would be to manually convert your numbers to fixed length hex numbers for output and manually convert them back for input. I don't know if C/C++'s treatment of null as a terminator would cause problems if you tried working with raw binary numbers.

So far I've been trying to avoid reading about C++ file streams, partly because I actually don't care to learn C++, but I guess I'll try to read a little about it.

Regards,
Michael



    
This message has been edited by MCalkins on May 2, 2011 3:38 AM

Posted on May 2, 2011, 3:33 AM

Respond to this message   

Return to Index

Response TitleAuthor and Date
Well, this one outputs binaryUnseen on May 2
 Yes, it does.Michael Calkins on May 2
  Erm..Unseen on May 2
   This one seems to work :-)Michael Calkins on May 2
    You the man MC!Unseen Machine on May 2
     *ywMichael Calkins on May 2
    Here's my re-writeUnseen on May 2
     2nd Version -Unseen on May 2
      Nearly there...Unseen on May 2
       Re: Nearly there...Michael Calkins on May 2
        Thanks again MCUnseen on May 2
         arrays... (edited to fix post)Michael Calkins on May 3
          Multidimensional arrays and expandable arrays in C++ChronoKitsune on May 3
   fstream vs ifstream/ofstream (and other things about your code)ChronoKitsune on May 2
    Re: fstream vs ifstream/ofstream (and other things about your code)Michael Calkins on May 2
     (FIXED) Well, I'm not sure how well defined the behaviour is, but...ChronoKitsune on May 3
      *Thank you.Michael Calkins on May 3

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement