Read file with unicode

This is a discussion on Read file with unicode within the C++ Programming forums, part of the General Programming Boards category; When i try to read from the file 1.txt (contains only "hello" ) fread returns 6 bytes but there is ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749

    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);
    }
    Compiler MSVC++ 2010 with Code::Blocks.

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

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    You mean wcout?

    I tried too without any result, so the problem lies elsewhere.
    Compiler MSVC++ 2010 with Code::Blocks.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Read file with unicode
    Does wcout read files?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Ok i should have posted it in the C board, sorry.
    Compiler MSVC++ 2010 with Code::Blocks.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Eh ???

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

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    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
    Location
    France
    Posts
    749
    Thanks a lot Bubba, its working with wifstream.
    Compiler MSVC++ 2010 with Code::Blocks.

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, 01:01 PM
  2. File read in problem
    By DimensionX in forum C Programming
    Replies: 5
    Last Post: 12-02-2009, 11:37 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21