Archived

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

C++ fstream and cout

This topic is 4958 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

Hello everybody, how are you all doing!? i have a little problem, when im working on an encryption algorithm thing and im facing a problem that is not really related to the alg. itslef, well here is a code of what im taking about: #include <iostream.h> #include
void main()
{
	ofstream outFile("output.txt", ios::trunc);
	char c;
	for(int i=0; i <256; i++)
	{	c=(char)i;
		cout<<i<<") "<<char(i)<<endl;
		outFile<<i<<") "<<char(i)<<endl;
	}
	outFile.close();
}
im facing the problem that the value of char(i) [for smaller number anyways; under 35 i believe] that C++ displaces from the cout is actually different from what it writes to file, reading back from the file and retrieveing the same data as it was entered is impossible cause some characters end up having the same symbol in the file. any idea how i can work this out?! thanx in advance. [edited by - stealthmaker on May 17, 2004 3:56:43 PM] [edited by - stealthmaker on May 17, 2004 3:58:46 PM]

Share this post


Link to post
Share on other sites
I really have no idea what your question is. However, it seems like you are trying to do binary output on a file stream, but didn''t open the file in binary mode. This is a bad thing.

Share this post


Link to post
Share on other sites
This has nothing to do with C++ and everything to do with character encodings.

Go look at taht file using like edit.com or whatever in a dos box and it will look exactly like the one you want.

Share this post


Link to post
Share on other sites
The lower part of the ASCII character set (less than 32) is composed of control characters that are not meant to be printed. Glyphs are provided in most fonts, but they aren''t necessarily the smiley faces DOS uses. There is no set glyphs for that section of the ASCII table.

Share this post


Link to post
Share on other sites
quote:
Original post by stealthmaker
any idea how i can work this out?!
For starters, use the correct headers. <iostream.h> and <fstream.h> are non-standard, so there''s no guarantee exactly what is in them; use <iostream> and <fstream> instead.

Entities in standard headers are inside the std namespace, so you''ll either need to fully qualify them at each use (eg std::cout << i << std::endl;) or introduce them via the using directive (eg using std::cout, using std::endl, or using namespace std to introduce all of them).

Replace ios::trunc with ios_base::trunc.

Share this post


Link to post
Share on other sites