hello all,

I'm getting some unexpected behavior from the ifstream object when using the open method.

im reading the following characters from a text file:

###############
#     #X#     #
# ### # # ### #
# #         # #
# #  ## ##  # #
# WWWW###WWWW #
#    CWWWC    #
#WWWWWWWWWWWWW#
#    CWWWC    #
# L WW   WW L #
# #         # #
# #         # #
# ### # # ### #
#B    #S#    B#
###############

i'm using the following code to load the file.

ifstream file;
file.open( file_name.c_str(), ios::out, ios::binary );

when i debug my project and look at the buffer with the text visualizer i get the following result:

###############
#     #X#     #
# ### # # ### #
# #         # #
# #  ## ##  # #
# WWWW###WWWW #
#    CWWWC    #
#WWWWWWWWWWWWW#
#    CWWWC    #
# L WW   WW L #
# #         # #
# #         # #
# ### # # ### #
#B    #S#    B#
################
#B    #S#    B#
###############

mind the last 2 rows.

Can anyone tell me what i'm doing wrong?

Recommended Answers

All 10 Replies

[B]ifstream[/B] file;
file.open( file_name.c_str(), ios::[B]out[/B][B],[/B] ios::binary );

shouldn't that be:

[B]ifstream[/B] file;
file.open( file_name.c_str(), ios::[B]in[/B] [B]|[/B] ios::binary );

I only need to read from the stream so i figured ios::out would be sufficient.

Tried it anyways, but got same result.

thx for the reply though

Can you post your code please?

sure, this is some code i used to debug and test, but with this code i got te unexpected behavior. pritty straight forward.

level::level( string file_name )
{
	ifstream file;
	file.open( file_name.c_str(), ios::out, ios::binary );

	platform pf;
	pf.platform_heigth = 15;
	pf.platform_width = 15;
	pf.init_platform_maze();
	for( int x = 0; x <= pf.platform_width; x++ )
		for( int y = 0; y <= pf.platform_heigth; y++ )
			pf.blocks[x][y] = file.get();


	for( int x = 0; x <= pf.platform_width; x++ )
		for( int y = 0; y <= pf.platform_width; y++ )
				cout << pf.blocks[x][y];

	cout << endl;
}

pf.blocks is a 2D vector
the result in console is correct though because i have a limited loop.

ifstream implies ios::in, so the ios::out should be ignored (have no effect) on the call to fopen.

Why use binary mode, when you're just reading in characters?

the code i posted is a result of multiple test where i tried different things.

i tried the following:

ifstream file;
	file.open( file_name.c_str() );

	ifstream file( file_name.c_str() );

	ifstream file;
	file.open( file_name.c_str(), ios::out, ios::binary );

	ifstream file;
	file.open( file_name.c_str(), ios::in, ios::binary );

	ifstream file;
	file.open( file_name.c_str(), ios::out );

	ifstream file;
	file.open( file_name.c_str(), ios::in );

all with the same result in the debugger;

All of those should open an fstream for input, only input.

The arguments to the .open( ) command are the file name, the opening mode, and the protection. So, your third argument is probably being ignored on a Windows system. The ios::out is, as I said, not appropriate to an ifstream, and should have no effect.

When you want to pass multiple modes to an .open( ) command, you need to join them with the bitwise OR operator, like:

fstream f;
f.open( "junk.txt", ios::in | ios::out | ios::ate );

ok, thx for pointing this out, usefull!
but not the awnser to my question.

I'm not seeing the problem. Changing your 2D vector to a 2D char array ('cause I don't have your full platform class), I get correct output for that given input. I don't see where the spurious # comes from in your output. I don't see anything wrong in your last two rows, but the one above that has the extra symbol.

Want to give us updated code and results?

have you tried removing the <= from for( int x = 0; x <= pf.platform_width; x++ ) you may be overwriting the bounds because going to the pf.platform_width with it being equal to 15 will actual give you 16 loops try

for( int x = 0; x < pf.platform_width; x++ )
           for( int y = 0; y < pf.platform_heigth; y++ )

if i could also suggest i would use different variables for the two separate for loops or just output right after receiving the variables such as

for( int x = 0; x < pf.platform_width; x++ )
{
           for( int y = 0; y < pf.platform_heigth; y++ )
           {
                       pf.blocks[x][y] = file.get();
                       cout << pf.blocks[x][y];
            }
}
cout << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.