views:

223

answers:

2

Hello, I'm trying to load into string the content of file saved on the dics. The file is .CS code, created in VisualStudio so I suppose it's saved in UTF-8 coding. I'm doing this:

FILE *fConnect = _wfopen(connectFilePath, _T("r,ccs=UTF-8"));
    if (!fConnect)
        return;
    fseek(fConnect, 0, SEEK_END);
    lSize = ftell(fConnect);
    rewind(fConnect);

    LPTSTR lpContent = (LPTSTR)malloc(sizeof(TCHAR) * lSize + 1);
    fread(lpContent, sizeof(TCHAR), lSize, fConnect);

But result is so strange - the first part (half of the string is content of .CS file), then strange symbols like 췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍 appear. So I think I read the content in a wrong way. But how to do that properly? Thank you so much and I'm looking to hear!

+1  A: 

Does the string contain all the contents of the cs file and then additional funny characters? Probably it's just not correctly null-terminated since fread will not automatically do that. You need to set the character following the string content to zero:

lpContent[lSize] = 0;
stephan
I tried it but it didn't help.
Seacat
+1  A: 

ftell(), fseek(), and fread() all operate on bytes, not on characters. In a Unicode environment, TCHAR is at least 2 bytes, so you are allocating and reading twice as much memory as you should be.

I have never seen fopen() or _wfopen() support a "ccs" attribute. You should use "rb" as the reading mode, read the raw bytes into memory, and then decode them once you have them all available, ie:

FILE *fConnect = _wfopen(connectFilePath, _T("rb")); 
if (!fConnect) 
  return; 
fseek(fConnect, 0, SEEK_END); 
lSize = ftell(fConnect); 
rewind(fConnect); 

LPBYTE lpContent = (LPBYTE) malloc(lSize); 
fread(lpContent, 1, lSize, fConnect);
fclose(lpContent);

.. decode lpContent as needed ...
free(lpContent); 
Remy Lebeau - TeamB
I don't understand how to decode it. All I need is to get the content of file as unicode string (actually I want to copy it into buffer). The length of taken lpContent is only half of previous lpContent, is it normal?
Seacat
Hurrah! I did it! Thanks a lot!!!
Seacat