Thread: Read file with unicode

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    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);
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The stream I/O in C++ supports wide characters. I would recommend using them.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    You mean wcout?

    I tried too without any result, so the problem lies elsewhere.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Read file with unicode
    Does wcout read files?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ok i should have posted it in the C board, sorry.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Eh ???

  7. #7
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    In the board for C programming, not C++

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks a lot Bubba, its working with wifstream.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  2. File read in problem
    By DimensionX in forum C Programming
    Replies: 5
    Last Post: 12-02-2009, 12:37 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM