#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
void Init()
{
const char *locale = "zh_CN.utf8";
// 根据环境变量设置locale
setlocale(LC_CTYPE, locale);
}
int to_wchar(wchar_t* &dest, char* &src,int n)
{
return mbstowcs(dest, src, n);
}
int main(int args, char **argv)
{
int handle=open(argv[1], O_CREAT|O_RDWR,S_IREAD|S_IWRITE);
int dsize = lseek(handle, 0, SEEK_END);
printf("dsize:%d\n", dsize);
Init();
std::ifstream fin(argv[1], std::ios::in);
const static int MAXCL = 1024*3;
char *line = new char[MAXCL];
wchar_t *wline = new wchar_t[MAXCL];
while(fin.getline(line, MAXCL))
{
int clen = strlen(line);
int wlen = to_wchar(wline, line, clen);
printf("len:%d\twlen:%d\tvalue:%s\n", clen, wlen, line);
for (int i=0; i<wlen; i++)
{
printf("%d\t%X\n", i, wline[i]);
}
}
fin.clear();
fin.close();
return 0;
}
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
#include <iostream>
using namespace std;
void Init()
{
const char *locale = "zh_CN.utf8";
// 根据环境变量设置locale
setlocale(LC_CTYPE, locale);
}
int to_wchar(wchar_t* &dest, char* &src,int n)
{
return mbstowcs(dest, src, n);
}
int main()
{
char* str1 = "abc123";
char* str2 = "abc124";
wchar_t *w_str1 ;
wchar_t *w_str2 ;
w_str1 = new wchar_t[20];
w_str2 = new wchar_t[20];
Init();
to_wchar(w_str1,str1, strlen(str1));
to_wchar(w_str2,str2,strlen(str2));
//wcout <<"wsize: "<<sizeof(w_str[0]) <<w_str[0] << "--" << w_str[1] << "--" << w_str[2]<<endl;
wcout<<"w_str: "<<w_str1<<endl;
wcout<<"w_str: "<<w_str2<<endl;
wcout<<"cmp: "<<wcsncmp(w_str1, w_str2, 3)<<endl;
delete(w_str1);
delete(w_str2);
return 0;
}