C 字符串特殊字符(葡萄牙语)

C string special character (Portuguese)

如何使用 C 中的字符串正确处理特殊的葡萄牙语字符,例如:ç、é、è 等...?

我找到了如何使用 printf 来完成它,但是 scanf、fgets 等等...我现在不知道如何在字符串上正确存储这种字符...

#include <locale.h>

int main (void){
  setlocale(LC_ALL,"Portuguese");

  printf("This is a example! Portuguese caracters ç é");

}

编辑:

按照下面的建议尝试了此代码:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>


int main() {
    int a = 0;
    setlocale(LC_ALL,"Portuguese");

    wprintf(L"Exemplo de ç\n");

    return 0;
}

从控制台手动编译它:gcc -o main.exe main.c 可以。 但是使用 devc++ 给我一个错误:

[错误] 转换为执行字符集:参数无效

如果我只输入:wprintf(L"Exemplo de\n"); (没有 ç)devc++ 现在可以很好地编译。

所以一旦手动编译它就可以工作了,我想这与 devc++ 编译选项有关...有人知道吗?

编辑2:

我所有这一切的主要目标是要求用户输入。读取该输入以保存到文件中。每次程序启动时,我都会读取文件以恢复保存在文件中的值。

但在葡萄牙语中,用户可以键入 ç、é、è 等内容...

本地字符使用宽字符串。以下对我有用:

#include <wchar.h>
#include <locale.h>
int main (void)
{
   setlocale(LC_ALL,"pl_PL.UTF-8");
   wprintf(L"This is a example! Polish characters ąśćłźó\n");
}

注意:

setlocale(LC_ALL,"Portuguese");

适用于终端输出(例如 printf)

如果您对使用特殊字符(例如葡萄牙语)从磁盘读取文件感兴趣, 特别是如果您使用 WINDOWS 和 DOS 文件(windows/DOS 中的文本文件), 您应该以 ANSI 编码编写文件。

如何以 ANSI 编码保存文本文件?

使用具有菜单选项“enconding”的“Notepad++”(https://notepad-plus-plus.org/) 允许 select 并以 ANSI 编码保存文件。

如果您使用 linux 或 OS/X,只需在源文件和终端中使用 UTF-8 编码即可无缝工作。

有了这个简单的源代码:

#include <stdio.h>

int main() {
    printf("Olá! vocês todos moram na França?\n");
    return 0;
}

我得到这个输出:Olá! vocês todos moram na França?

十六进制和八进制:

0000    4F   6C   C3   A1   21   20   76   6F
         O    l 3 1    !         v    o
0008    63   C3   AA   73   20   74   6F   64
         c 3 2    s         t    o    d
0020    6F   73   20   6D   6F   72   61   6D
         o    s         m    o    r    a    m
0028    20   6E   61   20   46   72   61   6E
              n    a         F    r    a    n
0040    C3   A7   61   3F   0A
      3 7    a    ?   \n

如您所见,á 编码为 \xC3\xA1ê 编码为 \xC3\xAAç 编码为 \xC3\xA7

How can I correctly handle special Portuguese characters like: ç, é, è and so on... using string in C?

将区域设置设置为使用 utf8 并使用 u8 前缀形成字符串文字。

#include <stdio.h>
#include <locale.h>
int main (void) {
  if (setlocale(LC_ALL, "en_US.utf8")) {
    puts("Unable to set locale");
  } else {
    puts(u8"This is a example! Portuguese characters ç é");
  }
}

输出

This is a example! Portuguese characters ç é

使用 wprintf() 的一个问题是所有输出都应该是 w....()。一旦代码使用 printf()wprintf(),流现在有一个 方向

Byte input/output functions shall not be applied to a wide-oriented stream and wide character input/output functions shall not be applied to a byte-oriented stream. C17dr § 7.21.2 4

您可以像其他人一样使用 locale.h。但是如果你想节省一些存储空间或使你的代码 运行 更快,你可以使用字符的 ASCII 含义。一个例子:

#include <stdio.h>
int main()
{
    printf("Oo! Special character: %c",141);
    return 0;
}
#include <iostream>
#include <locale>
using std::string;
using std::cout;
int main()
{
    setlocale(LC_ALL, "pt_BR.UTF-8");
    cout << "Agora pode usar : á é í ó ú à è ì ò ù ã õ â ê î ô û ç";
    cout << "Agora pode usar : Á É Í Ó Ú À È Ì Ò Ù Ã Õ Â Ê Î Ô Û Ç";
}

screenshot