Я использовал для программирования окон, но я хочу попробовать свои силы в создании кросс-платформенного приложения. И у меня есть некоторые вопросы, если вы не возражаете:
Вопрос 1
Есть ли способ открыть файл UNICODE\ASCII и автоматически определить его кодировку с использованием ANSI C. MSDN говорит, что fopen() может переключаться между различными форматами UNICODE (utf-8, utf-16, UNICODE BI\LI), если Я буду использовать флаг "ccs = UNICODE". Экспериментально было установлено, что переход от UNICODE к ASCII не происходит, но, пытаясь решить эту проблему, я обнаружил, что текстовые файлы Unicode имеют некоторые префиксы, такие как 0xFFFE, 0xFEFF или 0xFEBB.
FILE *file;
{
__int16 isUni;
file = _tfopen(filename, _T("rb"));
fread(&(isUni),1,2,file);
fclose(file);
if( isUni == (__int16)0xFFFE || isUni == (__int16)0xFEFF || isUni == (__int16)0xFEBB)
file = _tfopen(filename, _T("r,ccs=UNICODE"));
else
file = _tfopen(filename, _T("r"));
}
Итак, могу ли я сделать что-то вроде этой кросс-платформенной и не такой уродливой?
Вопрос 2
Я могу сделать что-то подобное в Windows, но будет ли он работать в Linux?
file = fopen(filename, "r");
fwscanf(file,"%lf",buffer);
Если нет, то есть ли какая-то функция ANSI C для преобразования строк ASCII в Unicode? Я хочу работать со строками Unicode в своей программе.
Вопрос 3
Кроме того, мне нужно выводить строки Unicode в консоль. В windows есть setlocale (*), но что делать в Linux? Кажется, что консоль уже есть Unicode.
Вопрос 4
Вообще говоря, я хочу работать с Unicode в своей программе, но я столкнулся с некоторыми странными проблемами:
f = fopen("inc.txt","rt");
fwprintf(f,L"Текст"); // converted successfully
fclose(f);
f = fopen("inc_u8.txt","rt, ccs = UNICODE");
fprintf(f,"text"); // failed to convert
fclose(f);
P.S. Есть ли хорошая книга о кросс-платформенном программировании, что-то со сравнением окон и кода программ Linux? И некоторые книги о способах использования Юникода, практические методы, то есть. Я не хочу погружаться в обычную историю UNICODE BI\LI, меня интересуют конкретные библиотеки C/С++.