我有一个名为 output.txt 的文件,我想在其中打印根 (√) 符号。 这是我的程序
#include<stdio.h>
#include<conio.h>
void main(void)
{
FILE *fptr;
fptr = fopen("output.txt","w+"); //open the file to write
fprintf(fptr,"\xfb"); // \xfb is the hexadecimal code for root symbol
fclose(fptr);
}
但是当我运行程序时 (û) 打印在里面
最佳答案
您遇到的问题是因为您试图使用扩展 ASCII 集的一部分(即:值超过 127 的字符)。 代码页 是您设置的内容,因此如果设置了 8 位 ASCII 符号的 MSB,它可以映射到许多不同的代码页之一,具体取决于区域/语言环境、操作系统等(即: 希腊语、拉丁语等)。在大多数情况下,ASCII 字符通常被认为是 7 位的,忽略代码页启用位。
尝试使用扩展 ASCII 不是一种可移植的方法,因此您最好的选择是:
- 使用unicode
- 确保您的 C 编译器符合 C99。
下面的例子解决了原来的问题。
源代码
#include <stdio.h>
void main(void) {
FILE *fptr;
fptr = fopen("output.txt","w+"); //open the file to write
fprintf(fptr, "\u221A\n");
fclose(fptr);
}
样本运行的输出
√
引用资料
- 如何通过 C 程序打印扩展 ASCII 字符 127 到 160?,2014 年 4 月 16 日访问,
<https://stackoverflow.com/questions/16359225/how-to-print-extended-ascii-characters-127-to-160-in-through-a-c-program>
- Unicode 字符“SQUARE ROOT”(U+221A),2014 年 4 月 16 日访问,
<http://www.fileformat.info/info/unicode/char/221a/index.htm>
关于c - 如何在文件中用 c 打印根符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23110535/