943,619 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2637
  • C++ RSS
Feb 16th, 2009
0

wide character file streams

Expand Post »
I'm trying to read a Unicode file using std::wifstream. On wintel.

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. wifstream File("input.txt");
  10. wstring Line;
  11.  
  12. while(!File.eof() )
  13. {
  14. getline(File, Line);
  15.  
  16. wcout << Line << endl;
  17. }
  18. system("pause");
  19. return 0;
  20. }

And this is my output
C++ Syntax (Toggle Plain Text)
  1.  ■A s e q u e n c e o f l e x i c o n s t h a t m a k e u p a s
  2. e n t e n c e .
  3. Press any key to continue . . .

Now the spaces between each character and the '■' thing at the start arn't in the text file. What am I doing wrong?
Last edited by ganbree; Feb 16th, 2009 at 7:33 am.
Reputation Points: 17
Solved Threads: 0
Light Poster
ganbree is offline Offline
28 posts
since Apr 2007
Feb 16th, 2009
0

Re: wide character file streams

The "thing" at the start is probably a binary character that tells programs the file is UNICODE format -- its called "UNICODE signature". When you click this link scroll down the page until you find the section titled "unicode signature". Please feel free to read the rest of the page too
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Feb 18th, 2009
0

Re: wide character file streams

Click to Expand / Collapse  Quote originally posted by ganbree ...
I'm trying to read a Unicode file using std::wifstream. On wintel.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. wifstream File("input.txt");
  10. wstring Line;
  11.  
  12. while(!File.eof() )
  13. {
  14. getline(File, Line);
  15.  
  16. wcout << Line << endl;
  17. }
  18. system("pause");
  19. return 0;
  20. }

And this is my output
C++ Syntax (Toggle Plain Text)
  1.  ■A s e q u e n c e o f l e x i c o n s t h a t m a k e u p a s
  2. e n t e n c e .
  3. Press any key to continue . . .

Now the spaces between each character and the '■' thing at the start arn't in the text file. What am I doing wrong?
Well, at first blush (I haven't tested it) you may need to specify
C++ Syntax (Toggle Plain Text)
  1. getline<wchar_t>( File, Line );

Hope it helps,
Sean
Last edited by Ancient Dragon; Feb 18th, 2009 at 7:27 pm. Reason: corrected code tags
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need Help Rounding Off
Next Thread in C++ Forum Timeline: Eclipse for Java developers made to do C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC