Domanda

Friends,

I am a beginner in c++ and I am using vc 6.0 to write a file in utf-8 encoding.

I am using fwprintf function for this. but the file is coming in ANSI coding. can anybody tell me how to save a file in utf-8 using fwprintf().

Here is my code,

#include "stdafx.h"
#include "wchar.h"
#include "windows.h"
int main(int argc, char* argv[])
{
    FILE *file;
    file = fopen ("abc.txt","w");
    fwprintf(file, L"This is my utf-8 encoded file");
    fclose(file);
    WinExec("\"C:\\Program Files\\EditPlus 3\\editplus.exe\abc.txt", SW_SHOWNORMAL);

    return 0;
}
È stato utile?

Soluzione 2

If your compiler allows it you can replace "w" with "w,ccs=UTF-8" in fopen call.

Altri suggerimenti

fopen() does not support encodings in VC6, so you will have to manage the encoding manually in code instead, eg:

#include "stdafx.h"
#include "wchar.h"
#include "windows.h"

bool writeUtf8StrToFile(FILE *file, const wchar_t *str)
{
    if (!file) return false;
    int wlen = lstrlenW(str);
    if (wlen == 0) return true;
    int utf8len = WideCharToMultiByte(CP_UTF8, 0, str, wlen, NULL, 0, NULL, NULL);
    if (utf8len == 0) return false;
    char *utf8 = (char*) malloc(utf8len);
    if (!utf8) return false;
    utf8len = WideCharToMultiByte(CP_UTF8, 0, str, wlen, utf8, utf8len, NULL, NULL);
    if (utf8len == 0) return false;
    fwrite(utf8, 1, utf8len, file);
    free(utf8);
    return true;
}

int main(int argc, char* argv[])
{
    FILE *file = fopen ("abc.txt", "wb");
    if (file)
    {
        writeUtf8StrToFile(file, L"This is my utf-8 encoded file");
        fclose(file);
        WinExec("\"C:\\Program Files\\EditPlus 3\\editplus.exe\" abc.txt", SW_SHOWNORMAL);
    }
    return 0;
}

Alternatively (since you did say you are using C++ and not C, which the above code is):

#include "stdafx.h"
#include "wchar.h"
#include "windows.h"
#include <fstream>
#include <string>

bool writeUtf8StrToFile(std::ofstream &file, const std::wstring &str)
{
{
    if (!file.is_open()) return false;
    if (str.empty()) return true;
    int utf8len = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0, NULL, NULL);
    if (utf8len == 0) return false;
    std::string utf8(utf8len, '\0');
    WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), &utf8[0], utf8len, NULL, NULL);
    file << utf8;
    return true;
}

int main(int argc, char* argv[])
{
    std::ofstream file("abc.txt", std::ios::out | std::ios::binary);
    if (file.is_open())
    {
        writeUtf8StrToFile(file, L"This is my utf-8 encoded file");
        file.close();
        WinExec("\"C:\\Program Files\\EditPlus 3\\editplus.exe\" abc.txt", SW_SHOWNORMAL);
    }    
    return 0;
}

Alternatively, use the UTF-8 library from this article, eg:

#include "stdafx.h"
#include "wchar.h"
#include "windows.h"
#include <fstream>
#include <string>
#include <iterator>
#include "utf8.h"

int main(int argc, char* argv[])
{
    std::ofstream file("abc.txt", std::ios::out | std::ios::binary);
    if (file.is_open())
    {
        std::wstring utf16str = L"This is my utf-8 encoded file";
        std::string utf8str;
        utf8::utf16to8(utf16str.begin(), utf16str.end(), std::back_inserter(utf8str));
        file << utf8str;
        file.close();
        WinExec("\"C:\\Program Files\\EditPlus 3\\editplus.exe\" abc.txt", SW_SHOWNORMAL);
    }    
    return 0;
}

Alternatively, use the UTF-8 stream convertor from this article, eg:

#include "stdafx.h"
#include "wchar.h"
#include "windows.h"
#include <fstream>
#include "stxutif.h"

int main(int argc, char* argv[])
{
    std::wofstream fs("abc.txt", std::ios::out);
    if (file.is_open())
    {
        std::locale utf8_locale(std::locale(), new utf8cvt<false>);
        file.imbue(utf8_locale); 
        file << L"This is my utf-8 encoded file";
        file.close();
        WinExec("\"C:\\Program Files\\EditPlus 3\\editplus.exe\" abc.txt", SW_SHOWNORMAL);
    }    
    return 0;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top