Skip to main content
gamedev.net
🔒 Locked

Evil ifstream!

Started by Xtremehobo Sep 6, 2003 at 5:29 PM 5 replies 450+ views
Original Post
Xtremehobo
Xtremehobo
My ifstream object quits reading characters from a file into my buffer when it hits a TAB (0x9) character.
	ifstream fin;
	fin.open(filename);
	char buffer[2048];
	fin >> buffer;
	fin.close();
	MessageBox(NULL,buffer,"test",MB_OK);
the text document it''s trying to open looks like this:
matt	mypass	sysadmin
nick	hispass	guest

My MessageBox just prints out "matt" and that''s it. Any ideas? thanks. ----- Everyone but me has an interesting sig
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
Thunder_Hawk
Thunder_Hawk
Use binary mode. A tab is considered whitespace. It''s the default behavior of an istream to stop reading at whitespace when using the extraction (>>) operator.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
Thunder_Hawk
Thunder_Hawk

ifstream fin(filename, ios::binary);
ostringstream iss;
iss << fin.rdbuf();
fin.close();
MessageBox(NULL,iss.str().c_str(),"test",MB_OK);


That should work (although I can't remember if binary mode is necessary here for some odd reason).



______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________


[edited by - Thunder_Hawk on September 6, 2003 6:48:58 PM]
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
C-Junkie
C-Junkie
nope, not binary mode. (miniRANT: BINARY MODE ONLY DOES \r\n and \n TRANSLATION AND ONLY ON WINDOWS! (insert rare exceptions here))

OP, you want to read() the data, not extract>> it. the extraction operator for char[] and strings only reads up to whitespace. you want to read all of it.
Xtremehobo
Xtremehobo
ok, I''m using read and it''s working how I want it now. Thanks
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
sjelkjd
sjelkjd

ifstream fin(filename);
istreambuf_iterator begin(fin),end;
string buffer(begin,end);


Don''t use fixed size buffers!
Seriema
Seriema
Effective STL by Scott Meyers
Item 29 - "Consider istreambuf_iterators for character-by-character input."

std::ifstream inputFile(filename);
std::string fileData(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());


and there you have it...


But I think there's a way to stop streams from stopping at white spaces... just looked at http://www.cplusplus.com/ref/iostream/ and found this: ios_base::skipws and ios_base::flags(). So if my assumtions are correct then inputFile.flags( ios_base::skipws ); will make the stream skip whitespaces and read the whole thing with one >> extraction? anyway... you seem to have found what you wanted so it doesn't matter anyway


"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

[edited by - Seriema on September 7, 2003 5:09:22 PM]

[edited by - Seriema on September 7, 2003 5:09:41 PM]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.