读书人

malloc分配字符串空间,该怎么处理

发布时间: 2012-02-26 20:19:44 作者: rapoo

malloc分配字符串空间
tc用malloc分配字符串空间后怎么赋中文字符?中文字符是文件中读入的,保存在buffer中。

[解决办法]
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
char buffer[]= "中文字符 ";
char s[4]={0};
strncpy(s, buffer, 2); // 中 ,汉字是两个连续字符,所以拷贝两个字符过来就是了
puts(s);

system( "pause ");
return 0;
}


[解决办法]
#include <iostream>
#include <tchar.h>
using namespace std;

void main()
{
std::wcout.imbue(std::locale( "CHS "));
wchar_t buffer[] =L "中文字符 ";

wcout < < buffer;
system( "pause ");
}
[解决办法]
楼上的那位是C++了。
要显示中文字符必须要字模的。
[解决办法]
可以用memcpy来按字节拷贝,

char buffer[]= "中文字符 ";
char out[100];
memcpy(out,buffer,sizeof(buffer));

读书人网 >C语言

热点推荐