Problem reading a unicode text file
-
//I have a problem reading a text file which I have //created using Notepad and saved at c:\test.txt in //"Unicode" format. //The file contains just a single word: "Test" //The following is a C++ program to read the file #include #include using namespace std; typedef basic_ifstream ifs; void main () { ifs is ("c:\\test.txt"); wint_t c; while ((c = is.get()) != WEOF) { wprintf (L"%c", c); } } //The output was: //..T.e.s.t. //Note that the loop went for 10 times while I expected it //to run only 4 times for word "Test". //The following is an equivalent C program that does all correctly. /* #include void main () { FILE *fp = _wfopen (L"c:\\test.txt", L"rb"); wint_t c; while ((c = fgetwc(fp)) != WEOF) { wprintf (L"%c", c); } fclose (fp); } */ //Output: Test //Do you have any suggestions? Please help. Thanks in advance! B2C
-
//I have a problem reading a text file which I have //created using Notepad and saved at c:\test.txt in //"Unicode" format. //The file contains just a single word: "Test" //The following is a C++ program to read the file #include #include using namespace std; typedef basic_ifstream ifs; void main () { ifs is ("c:\\test.txt"); wint_t c; while ((c = is.get()) != WEOF) { wprintf (L"%c", c); } } //The output was: //..T.e.s.t. //Note that the loop went for 10 times while I expected it //to run only 4 times for word "Test". //The following is an equivalent C program that does all correctly. /* #include void main () { FILE *fp = _wfopen (L"c:\\test.txt", L"rb"); wint_t c; while ((c = fgetwc(fp)) != WEOF) { wprintf (L"%c", c); } fclose (fp); } */ //Output: Test //Do you have any suggestions? Please help. Thanks in advance! B2C
balu_codeproject wrote:
typedef basic_ifstream ifs;
Try:
typedef wifstream ifs;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
balu_codeproject wrote:
typedef basic_ifstream ifs;
Try:
typedef wifstream ifs;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Thanks for your help! I hope the problem is solved.
-
balu_codeproject wrote:
typedef basic_ifstream ifs;
Try:
typedef wifstream ifs;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Unicode Text File c:\test.txt contents: "Test" (without quotes). wchar_t is defined as unsigned short as it should be. typedef unsigned short wchar_t; But wifstream which is defined as typedef basic_ifstream > wifstream; cannot read unicode file correctly. #include using namespace std; void main () { wifstream is ("c:\\test.txt"); wchar_t c; while ((c = is.get ()) != WEOF) { wprintf (L"%c", c); } } output was: ..T.e.s.t. When I tried short instead of wchar_t all went ok; #include using namespace std; void main () { typedef basic_ifstream > ifs; ifs is ("c:\\test.txt"); wchar_t c; while ((c = is.get ()) != WEOF) { wprintf (L"%c", c); } } output was: Test Should wchar_t be defined as short or this is a bug in standard C++ library shipped with VC++ 6.0? B2C