我是一个初学者,我玩了winapi-C的FindFirstFileW()。这个单行代码的路径是:“ \\?\ c:\Français\”,我想将“*”连接到这条路径类型wchar_t(然后我将它用作FindFirstFileW()的arg)。Concat wchar_t C中的Unicode字符串?
我做了我的两个测试用例,第一个是ansi_string(),它似乎工作正常,第二个是unicode_string() - 我不太明白我应该如何将额外的“*”字符串到独特的路径。我将字符串写入文件,因为我无法将单脚编码字符打印到标准输出。
注意:我的目标是学习,这意味着我会欣赏指导和引用适当的资源问候我的情况,我非常初学者,这是我第一次尝试使用Unicode。
感谢,Doori酒吧
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <string.h>
#include <errno.h>
void *error_malloc(int size);
void ansi_string(char **str1, char **str2);
void unicode_string(wchar_t **wstr1, wchar_t **wstr2);
void unicode_string(wchar_t **wstr1, wchar_t **wstr2)
{
/* assign wstr1 with the path: \\?\c:\Français\ */
*wstr1 = error_malloc((wcslen(L"\\\\?\\c:\\Français\\")+1) *sizeof(**wstr1));
wcscpy(*wstr1,L"\\\\?\\c:\\Français\\");
/* concat wstr1+"*" , assign wstr2 with: \\?\c:\Français\* */
*wstr2 = error_malloc((wcslen(*wstr1) + 1 + 1) * sizeof(**wstr1));
/* swprintf(*wstr2,"%ls*",*wstr1); */
/* how should I concat wstr1+"*"? */
wcscpy(*wstr2,L"\\\\?\\c:\\Français\\");
}
void ansi_string(char **str1, char **str2)
{
/* assign str1 with the path: c:\English\ */
*str1 = error_malloc(strlen("c:\\English\\") + 1);
strcpy(*str1,"c:\\English\\");
/* concat str1+"*" , assign str2 with: c:\English\* */
*str2 = error_malloc(strlen(*str1) + 1 + 1);
sprintf(*str2,"%s*",*str1);
}
void *error_malloc(int size)
{
void *ptr;
int errornumber;
if ((ptr = malloc(size)) == NULL) {
errornumber = errno;
fprintf(stderr,"Error: malloc(): %d; Error Message: %s;\n",
errornumber,strerror(errornumber));
exit(1);
}
return ptr;
}
int main(void)
{
FILE *outfile;
char *str1;
char *str2;
wchar_t *wstr1;
wchar_t *wstr2;
if ((outfile = fopen("out.bin","w")) == NULL) {
printf("Error: fopen failed.");
return 1;
}
ansi_string(&str1,&str2);
fwrite(str2, sizeof(*str2), strlen(str2), outfile);
printf("strlen: %d\n",strlen(str2));
printf("sizeof: %d\n",sizeof(*str2));
free(str1);
free(str2);
unicode_string(&wstr1,&wstr2);
fwrite(wstr2, sizeof(*wstr2), wcslen(wstr2), outfile);
printf("wcslen: %d\n",wcslen(wstr2));
printf("sizeof: %d\n",sizeof(*wstr2));
free(wstr1);
free(wstr2);
fclose(outfile);
return 0;
}
感谢您的快速响应,这是我应用它的方式: wcscpy(* wstr2,* wstr1); wcscat(* wstr2,L“*”); Doori酒吧 – 2010-05-05 02:21:28