Advertisement

stupid c++, can someone help me?

Started by November 08, 2001 09:49 PM
9 comments, last by Useless Newbie 23 years, 3 months ago
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.
Instead of itoa() ...
int myInteger = 500;
char str[10];
sprintf(str, "%i", myInteger);
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
How does that help me output to a file?
How does that help me output to a file?
You write the char array "str" to the file.

-------
Andrew

It doesn''t. It helps convert integers to strings, which you can then output to the file.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
Ah, got it. Thanks.
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.
---------------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.
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()  

you can use stream objects. That way, you can output ints, chars, everything (that is defined)

This topic is closed to new replies.

Advertisement