Help with file I/O

Started by
9 comments, last by Lost 20 years ago
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]
Advertisement
hmm.... and the "File was opened" message is printed?

"Sneftel is correct, if rather vulgar." --Flarelocke
Yes, it prints the open message.
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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
-eldee;another space monkey;[ Forced Evolution Studios ]
eldee LOL.

I know you are looking for a defenite answer, but this text here is exactly what you need to look at to solve your problem. Take a look at how the example uses `is.read (buffer,length);''. I realy think this will help you.

Good luck
Rate me up.
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.
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.
I''ve been doing it like this and never had any problems:

MapOne.read((char*)&tempblah, sizeof(int)*100);
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 ); // gooddo_to_int_array( &array[0], 100 ); // gooddo_to_int_array( &array, 100 ); // bad but worksdo_to_int_array( pointer, 100 ); // gooddo_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.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement