-
Read file with unicode
When i try to read from the file 1.txt (contains only "hello" ) fread returns 6 bytes but there is nothing to print out from buf.
What am i doing wrong?
Code:
#include <wchar.h>
#include <iostream>
using namespace std;
int main()
{
FILE * file = _wfopen(L"1.txt", L"r+b");
int fileSize = 0;
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
rewind(file);
cout << "Filesize: " << fileSize << " bytes." << endl;
wchar_t * buf = (wchar_t *)calloc(260,sizeof(wchar_t));
size_t result = fread(buf, sizeof(wchar_t), fileSize, file);
printf( "%ls\n", buf );
cout << "fread returns: " << result << endl;
free(buf);fclose(file);
}
-
The stream I/O in C++ supports wide characters. I would recommend using them.
-
You mean wcout?
I tried too without any result, so the problem lies elsewhere.
-
Quote:
Read file with unicode
Does wcout read files?
-
Ok i should have posted it in the C board, sorry.
-
-
In the board for C programming, not C++
-
My point was that wcout does not read files. I was talking about wifstream which is an ifstream specialized for wchar_t. Your output will not be right if you are reading from the file incorrectly. My guess is you are doing exactly that.
-
Thanks a lot Bubba, its working with wifstream.