同时使用代码页 437 和 setlocale
Using code page 437 and setlocale at the same time
我想在打印 n 元树的函数中使用代码页 437 中的一些特殊字符,因此我可以这样做:
http://www.randygaul.net/wp-content/uploads/2015/06/Capture1.png
(基本上类似于linux中的tree
命令)
问题是,我的算法使用的 setlocale(LC_ALL, "Portuguese")
弄乱了那些特殊字符。我想知道我是否可以以某种方式将 C 默认语言环境单独应用于该函数。
只需保存当前语言环境,然后恢复:
void func_with_my_own_locale(void) {
const char * localesave = setlocale(LC_ALL, NULL);
assert(localesave != NULL); // or some fprintf(stderr, ....);
if (setlocale(LC_ALL, "CP437" /* or "" */) == NULL) {
assert(0);
}
......
if (setlocale(LC_ALL, localesave) == NULL) {
assert(0);
}
}
注意,语言环境在进程中的所有线程之间共享,因此您需要在调用此类函数时暂停所有其他线程(或确保它们不调用任何与语言环境相关的函数)。
Upon successful completion, setlocale() shall return the string associated with the specified category for the new locale. Otherwise, setlocale() shall return a null pointer and the program's locale is not changed.
A null pointer for locale causes setlocale() to return a pointer to the string associated with the category for the program's current locale.
The string returned by setlocale() is such that a subsequent call with that string and its associated category shall restore that part of the program's locale.
我想在打印 n 元树的函数中使用代码页 437 中的一些特殊字符,因此我可以这样做:
http://www.randygaul.net/wp-content/uploads/2015/06/Capture1.png
(基本上类似于linux中的tree
命令)
问题是,我的算法使用的 setlocale(LC_ALL, "Portuguese")
弄乱了那些特殊字符。我想知道我是否可以以某种方式将 C 默认语言环境单独应用于该函数。
只需保存当前语言环境,然后恢复:
void func_with_my_own_locale(void) {
const char * localesave = setlocale(LC_ALL, NULL);
assert(localesave != NULL); // or some fprintf(stderr, ....);
if (setlocale(LC_ALL, "CP437" /* or "" */) == NULL) {
assert(0);
}
......
if (setlocale(LC_ALL, localesave) == NULL) {
assert(0);
}
}
注意,语言环境在进程中的所有线程之间共享,因此您需要在调用此类函数时暂停所有其他线程(或确保它们不调用任何与语言环境相关的函数)。
Upon successful completion, setlocale() shall return the string associated with the specified category for the new locale. Otherwise, setlocale() shall return a null pointer and the program's locale is not changed.
A null pointer for locale causes setlocale() to return a pointer to the string associated with the category for the program's current locale.
The string returned by setlocale() is such that a subsequent call with that string and its associated category shall restore that part of the program's locale.