Archived

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

Help with file I/O

This topic is 4991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to load info from a raw file. But when I check to see what was loaded into the array, I'm just getting the trash info that was in there from the beginning.
int tempblah[100];

fstream MapOne;
MapOne.open("map.raw",ios::in|ios::binary);
if (MapOne.is_open()){
	cout << "File was openned\n";
	MapOne.read(reinterpret_cast<char*>(&tempblah), sizeof (int)*100);
}

MapOne.close();

cout << tempblah[0];
cout << "\n"; 
//edit: changed "code" tags to "source" tags so that the reinterpret_cast angle brackets would not be interpretted as an html tag [edited by - lessbread on November 7, 2003 7:26:23 PM]

Share this post


Link to post
Share on other sites
hmm.... and the "File was opened" message is printed?


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Try it without placing an address of operator in front of tempblah. That changes the cast from pointer to int to pointer to char to pointer to pointer to int to pointer to char.

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
That changes the cast from pointer to int to pointer to char to pointer to pointer to int to pointer to char.


wtf? heh

Share this post


Link to post
Share on other sites
What LessBread meant was...

int x[100];

then if I just use x as a variable.. it represents the address of the first element of the array... so if you use address of on x... ie &x, you get a pointer to the address of the first array element, and you can''t cast a pointer to a pointer... to a pointer.

Share this post


Link to post
Share on other sites
I''m still having problems.
I know that the file I''m trying to read in is 100 bytes long.

LessBread, you were right about the address of operator, I had been looking at example for dealing with loading a class.

What/how is most common way most people read binary files?
I''ve been trying to use most c++ way, but I''m thinking it''s not worth the headache & should just use easier way. Most of the ref books I have use different way in each book. I''m "trying" to use Object-Oriented Programming in C++ (4th edition) Robert Lafore as my main ref, since he seems to use the most purely c++ way.

Share this post


Link to post
Share on other sites
First: for arrays, "array" and "&array" is almost identical, whereas for pointers, "pointer" and "&pointer" is very different. This is somewhat confusing, but you''ll have to learn to deal with it. The recommended way of doing it is to take the address of the first element, or use only the name:


int array[ 100 ];
int * pointer = new int[ 100 ];
do_to_int_array( array, 100 ); // good
do_to_int_array( &array[0], 100 ); // good
do_to_int_array( &array, 100 ); // bad but works
do_to_int_array( pointer, 100 ); // good
do_to_int_array( &pointer, 100 ); // really bad (won''t compile, hopefully)


Second: if your file is 100 BYTES, and you''re reading 100 INTS into the array, then you will only read data into the first 25 elements, because sizeof(int) is 4 and sizeof(byte) is 1.

Share this post


Link to post
Share on other sites
hiya..
i am having the same problem as lost..
lost, if u have solved the problem, may i know how u go about it?
anyone can offer a better explanation?

thanks

Share this post


Link to post
Share on other sites