CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: A question regarding reading unicode

  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding reading unicode

    Basically I am trying to read unicode from a text file and this text file is saved as ansi code. Here is the code,
    Code:
      int main()
      {
    	TCHAR line[256];
    	FILE* file = _tfopen(_T("C:\\Temp\\test.txt"), _T("r"));
    
    	if(!file)
    		return 1;
    
    	int len;
    
    	while(_fgetts(line, 256, file))
    	{
    		...
    	}
    
            return 0;
      }
    If test.txt is something like the following,
    Code:
    test1
    test2
    test3
    Then when I read first line, the string always looks like L't', L'e', L's', L't', L'1', L'', 0. There is always one more character L'' is attached. I don't know why? Is there anything I should consider when reading unicode from file. Besides, if I save test.txt as unicode and then I read test.txt same way, then I get a messy output. I couldn't figure out why? Thanks.

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: A question regarding reading unicode

    _fgetts(), as well as fgets(), include the newline character.

    This is why it is customary to trim the buffer, after it is read with _fgetts().

    For instance:
    Code:
    line[_tcslen(line) - 1] = _T('\0');

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)