Archived

This topic is now archived and is closed to further replies.

Useless Newbie

stupid c++, can someone help me?

Recommended Posts

Useless Newbie    122
here''s my problem, I have a list of objects, and I want to iterate through the list and save the objects attributes to a file. However, all the data are integers, and the output stream only accepts char arrays to write to the file. My compilier doen''t support the itoa function, and for the life of me I can''t figure out how to stream ints into the file. Can someone please help. I''ve already lost four hours of my life that I''ll never get back.

Share this post


Link to post
Share on other sites
sleeping    122
How about you try to cast your integers element with a pointer to a char element? Try using something like:

(char*)&integerVariable

I hope it helps you.

Battle doesn''''t need a purpose, the battle is its own purpose. You don''''t ask why a field burns or a plague spreads, don''''t ask why I fight.

Share this post


Link to post
Share on other sites
invective    118
To output binary instead of text:
  

typedef struct info {
int x,y,z;
};

info myinfo;

std::ofstream file;
file.open ("out.dat", std::ios::binary );
file.write ( (char *)(&myinfo), sizeof (info) );
file.close()


Share this post


Link to post
Share on other sites
smart_idiot    1298
I''d use fwrite(&number,sizeof(number),1,file) (make sure the file was opened for writing in binary) and if I couldn''t, i''d do this:


char a,b,c,d;
a = number & 0xff;
b = (number >> 8) & 0xff;
c = (number >> 16) & 0xff;
d = (number >> 24) & 0xff;


and save those 4 characters. But I can so I won''t.

P.S. to undo that you would do this:


number = a + (b << 8) + (c << 16) + (d << 24);

Share this post


Link to post
Share on other sites