C语言 const char*转化为const wchar_t*

当使用EasyX图形库输出汉字字符遇到乱码问题时,可以通过在汉字字符串前加上_T或TEXT进行宽字符处理。例如,使用const wchar_t*类型定义字符串数组,并在每个字符串前加上_T宏,如_T(哈尔滨),这样调用outtextxy函数时,汉字就能正常显示而不会出现乱码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       在使用EasyX图形库时,我们常需要将字符输出到图形界面,这时候就要用到outtextxy函数了。但是像下面这样的字符类型,outtextxy函数并不支持。

const char  *info[]={"哈尔滨","长春","沈阳","呼和浩特","北京", "天津",
				"石家庄" "济南", "太原","西安","兰州","乌鲁木齐","西宁",
                "拉萨","成都","郑州","南京","合肥","武汉","重庆","贵阳",
				"长沙","南昌","上海","杭州","福州","昆明","南宁","广州","海口"}; 

         可能会有小伙伴会用(const wchar_t*)强制类型转换,但是输出的结果却是乱码的。这时候就可以在汉字字符前加上_T或者TEXT,如下图:

const wchar_t* info[] = { _T("哈尔滨"),_T("长春"),_T("沈阳"),_T("呼和浩特"),_T("北京"), _T("天津"),
				_T("石家庄"), _T("济南"), _T("太原"),_T("西安"),_T("兰州"),
				_T("乌鲁木齐"),_T("西宁"),_T("拉萨"),_T("成都"),_T("郑州"),
				_T("南京"),_T("合肥"),_T("武汉"),_T("重庆"),_T("贵阳"),
				_T("长沙"),_T("南昌"),_T("上海"),_T("杭州"),_T("福州"),
				_T("昆明"),_T("南宁"),_T("广州"),_T("海口") };

这样outtextxy(position_x, position_y, info[i])输出的汉字字符就不会乱码啦。

使用C语言、vscode软件、g++编译器、utf-8编码环境 WJ文件中有三个文件,运行WJ文件 WJ文件的三个子文件有以下内容: Recipe.h文件:#ifndef RECIPE_H #define RECIPE_H #define MAX_NAME_LEN 50 #define MAX_CATEGORY_LEN 30 #define MAX_INGREDIENTS_LEN 200 #define MAX_STEPS_LEN 500 #define MAX_RECIPES 100 #define FILENAME "recipes.dat" // 菜谱结构体 typedef struct { int id; // 唯一标识符 char name[MAX_NAME_LEN]; char category[MAX_CATEGORY_LEN]; char ingredients[MAX_INGREDIENTS_LEN]; char steps[MAX_STEPS_LEN]; } Recipe; // 函数声明 void init_system(); int add_recipe(const char *name, const char *category, const char *ingredients, const char *steps); int delete_recipe(int id); int modify_recipe(int id, const char *name, const char *category, const char *ingredients, const char *steps); Recipe* find_recipe_by_id(int id); Recipe* find_recipe_by_name(const char *name); void list_all_recipes(); Recipe* get_random_recipe(); void save_to_file(); void load_from_file(); #endif Recipe.c文件:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include "Recipe.h" // 全局变量 Recipe recipes[MAX_RECIPES]; int recipe_count = 0; int next_id = 1; // 初始化系统 void init_system() { load_from_file(); } // 添加菜谱 int add_recipe(const char *name, const char *category, const char *ingredients, const char *steps) { if (recipe_count >= MAX_RECIPES) return 0; Recipe *r = &recipes[recipe_count]; r->id = next_id++; strncpy(r->name, name, MAX_NAME_LEN); strncpy(r->category, category, MAX_CATEGORY_LEN); strncpy(r->ingredients, ingredients, MAX_INGREDIENTS_LEN); strncpy(r->steps, steps, MAX_STEPS_LEN); recipe_count++; save_to_file(); return r->id; } // 删除菜谱 int delete_recipe(int id) { for (int i = 0; i < recipe_count; i++) { if (recipes[i].id == id) { // 移动数组元素 for (int j = i; j < recipe_count - 1; j++) { recipes[j] = recipes[j + 1]; } recipe_count--; save_to_file(); return 1; } } return 0; } // 修改菜谱 int modify_recipe(int id, const char *name, const char *category, const char *ingredients, const char *steps) { for (int i = 0; i < recipe_count; i++) { if (recipes[i].id == id) { if (name) strncpy(recipes[i].name, name, MAX_NAME_LEN); if (category) strncpy(recipes[i].category, category, MAX_CATEGORY_LEN); if (ingredients) strncpy(recipes[i].ingredients, ingredients, MAX_INGREDIENTS_LEN); if (steps) strncpy(recipes[i].steps, steps, MAX_STEPS_LEN); save_to_file(); return 1; } } return 0; } // 通过ID查找 Recipe* find_recipe_by_id(int id) { for (int i = 0; i < recipe_count; i++) { if (recipes[i].id == id) { return &recipes[i]; } } return NULL; } // 通过名称查找 Recipe* find_recipe_by_name(const char *name) { for (int i = 0; i < recipe_count; i++) { if (strstr(recipes[i].name, name) != NULL) { return &recipes[i]; } } return NULL; } // 列出所有菜谱 void list_all_recipes() { printf("\n==== 所有菜谱 (%d) ====\n", recipe_count); for (int i = 0; i < recipe_count; i++) { printf("[ID:%d] %s (%s)\n", recipes[i].id, recipes[i].name, recipes[i].category); } printf("=====================\n"); } // 随机推荐菜谱 Recipe* get_random_recipe() { if (recipe_count == 0) return NULL; srand(time(NULL)); return &recipes[rand() % recipe_count]; } // 保存到文件 void save_to_file() { FILE *file = fopen(FILENAME, "wb"); if (!file) return; // 写入数据 fwrite(&recipe_count, sizeof(int), 1, file); fwrite(&next_id, sizeof(int), 1, file); fwrite(recipes, sizeof(Recipe), recipe_count, file); fclose(file); } // 从文件加载 void load_from_file() { FILE *file = fopen(FILENAME, "rb"); if (!file) return; // 读取数据 fread(&recipe_count, sizeof(int), 1, file); fread(&next_id, sizeof(int), 1, file); fread(recipes, sizeof(Recipe), recipe_count, file); fclose(file); } main.c文件:#include <stdio.h> #include <string.h> #include "Recipe.h" #include <locale.h> #include <wchar.h> #include <windows.h> int main() { // 设置控制台输出为UTF-8编码 SetConsoleOutputCP(65001); // 设置本地化环境,用于宽字符处理 setlocale(LC_ALL, "zh_CN.UTF-8"); init_system(); int choice; do{ printf("\n==== 家庭菜谱管理系统 ====\n"); printf("1. 添加菜谱\n"); printf("2. 删除菜谱\n"); printf("3. 修改菜谱\n"); printf("4. 查找菜谱(ID)\n"); printf("5. 查找菜谱(名称)\n"); printf("6. 列出所有菜谱\n"); printf("7. 随机推荐菜谱\n"); printf("8. 退出系统\n"); printf("请选择操作: "); canf("%d", &choice); getchar(); // 清除输入缓冲区 } while (choice != 8); return 0; { /* code */ } } // 显示菜单 // 添加菜谱界面 void add_recipe_ui() { char name[MAX_NAME_LEN]; char category[MAX_CATEGORY_LEN]; char ingredients[MAX_INGREDIENTS_LEN]; char steps[MAX_STEPS_LEN]; printf("\n--- 添加新菜谱 ---\n"); printf("菜谱名称: "); fgets(name, MAX_NAME_LEN, stdin); name[strcspn(name, "\n")] = 0; // 移除换行符 printf("菜谱类别: "); fgets(category, MAX_CATEGORY_LEN, stdin); category[strcspn(category, "\n")] = 0; printf("所需食材: "); fgets(ingredients, MAX_INGREDIENTS_LEN, stdin); ingredients[strcspn(ingredients, "\n")] = 0; printf("制作步骤: "); fgets(steps, MAX_STEPS_LEN, stdin); steps[strcspn(steps, "\n")] = 0; int id = add_recipe(name, category, ingredients, steps); if (id) { printf("添加成功! 菜谱ID: %d\n", id); } else { printf("添加失败! 已达最大菜谱数量\n"); } } // 显示菜谱详情 void display_recipe(Recipe *r) { if (!r) { printf("未找到菜谱!\n"); return; } printf("\n==== 菜谱详情 ====\n"); printf("ID: %d\n", r->id); printf("名称: %s\n", r->name); printf("类别: %s\n", r->category); printf("食材: %s\n", r->ingredients); printf("步骤: %s\n", r->steps); printf("=================\n"); } int main() { init_system(); int choice; do { display_menu(); scanf("%d", &choice); getchar(); // 清除输入缓冲区 switch (choice) { case 1: // 添加 add_recipe_ui(); break; case 2: { // 删除 int id; wprintf("输入要删除的菜谱ID: "); scanf("%d", &id); getchar(); if (delete_recipe(id)) { wprintf("删除成功!\n"); } else { wprintf("删除失败! 无效ID\n"); } break; } case 3: { // 修改 int id; wprintf("输入要修改的菜谱ID: "); scanf("%d", &id); getchar(); Recipe *r = find_recipe_by_id(id); if (!r) { wprintf("菜谱不存在!\n"); break; } char name[MAX_NAME_LEN]; char category[MAX_CATEGORY_LEN]; char ingredients[MAX_INGREDIENTS_LEN]; char steps[MAX_STEPS_LEN]; wprintf("新名称(回车保持原值): "); fgets(name, MAX_NAME_LEN, stdin); name[strcspn(name, "\n")] = 0; wprintf("新类别(回车保持原值): "); fgets(category, MAX_CATEGORY_LEN, stdin); category[strcspn(category, "\n")] = 0; wprintf("新食材(回车保持原值): "); fgets(ingredients, MAX_INGREDIENTS_LEN, stdin); ingredients[strcspn(ingredients, "\n")] = 0; wprintf("新步骤(回车保持原值): "); fgets(steps, MAX_STEPS_LEN, stdin); steps[strcspn(steps, "\n")] = 0; modify_recipe(id, *name ? name : NULL, *category ? category : NULL, *ingredients ? ingredients : NULL, *steps ? steps : NULL); wprintf("修改成功!\n"); break; } case 4: { // 按ID查找 int id; wprintf("输入菜谱ID: "); scanf("%d", &id); getchar(); display_recipe(find_recipe_by_id(id)); break; } case 5: { // 按名称查找 char name[MAX_NAME_LEN]; wprintf("输入菜谱名称: "); fgets(name, MAX_NAME_LEN, stdin); name[strcspn(name, "\n")] = 0; display_recipe(find_recipe_by_name(name)); break; } case 6: // 列出所有 list_all_recipes(); break; case 7: // 随机推荐 display_recipe(get_random_recipe()); break; case 8: // 退出 wprintf("感谢使用!\n"); break; default: wprintf("无效选择!\n"); } } while (choice != 8); return 0; } 在运行输入:g++ -o WJ Recipe.c main.c 出现以下错误:Microsoft Windows [版本 10.0.26100.4652] (c) Microsoft Corporation。保留所有权利。 C:\Users\34171\Desktop\code\WJ>g++ -o WJ Recipe.c main.c main.c: In function 'int main()': main.c:28:9: error: 'canf' was not declared in this scope; did you mean 'scanf'? 28 | canf("%d", &choice); | ^~~~ | scanf main.c: At global scope: main.c:89:5: error: redefinition of 'int main()' 89 | int main() { | ^~~~ main.c:8:5: note: 'int main()' previously defined here 8 | int main() | ^~~~ main.c: In function 'int main()': main.c:94:9: error: 'display_menu' was not declared in this scope; did you mean 'display_recipe'? 94 | display_menu(); | ^~~~~~~~~~~~ | display_recipe main.c:105:25: error: cannot convert 'const char*' to 'const wchar_t*' 105 | wprintf("杈D: "); | ^~~~~~~~~~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:110:29: error: cannot convert 'const char*' to 'const wchar_t*' 110 | wprintf("錸"); | ^~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:112:29: error: cannot convert 'const char*' to 'const wchar_t*' 112 | wprintf("錎\n"); | ^~~~~~~~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:119:25: error: cannot convert 'const char*' to 'const wchar_t*' 119 | wprintf("杈D: "); | ^~~~~~~~~~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:125:33: error: cannot convert 'const char*' to 'const wchar_t*' 125 | wprintf("鑞"); | ^~~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:134:25: error: cannot convert 'const char*' to 'const wchar_t*' 134 | wprintf("鎚); | ^~~~~~~~~~~~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:138:25: error: cannot convert 'const char*' to 'const wchar_t*' 138 | wprintf("鎚); | ^~~~~~~~~~~~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:142:25: error: cannot convert 'const char*' to 'const wchar_t*' 142 | wprintf("鎚); | ^~~~~~~~~~~~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:146:25: error: cannot convert 'const char*' to 'const wchar_t*' 146 | wprintf("鎚); | ^~~~~~~~~~~~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:156:25: error: cannot convert 'const char*' to 'const wchar_t*' 156 | wprintf("淇n"); | ^~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:162:25: error: cannot convert 'const char*' to 'const wchar_t*' 162 | wprintf("杈D: "); | ^~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:172:25: error: cannot convert 'const char*' to 'const wchar_t*' 172 | wprintf("杈m); | ^~~~~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:189:25: error: cannot convert 'const char*' to 'const wchar_t*' 189 | wprintf("鎛"); | ^~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ main.c:193:25: error: cannot convert 'const char*' to 'const wchar_t*' 193 | wprintf("鎛"); | ^~~~~~~~~~~~~ | | | const char* In file included from main.c:1: D:/TDM-GCC/x86_64-w64-mingw32/include/stdio.h:1093:29: note: initializing argument 1 of 'int wprintf(const wchar_t*, ...)' 1093 | int wprintf (const wchar_t *__format, ...) | ~~~~~~~~~~~~~~~^~~~~~~~ C:\Users\34171\Desktop\code\WJ>
最新发布
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是不是应该好好学习呢?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值