c中的utf8字符串和malloc

Jon*_*lli 3 c unicode malloc utf-8

使用"opendir"和"readdir",我会读取目录内容.在那个过程中我做了一些字符串操作/分配:类似的东西:

int stringlength = strlen(cur_dir)+strlen(ep->d_name)+2;
char *file_with_path = xmalloc(stringlength); //xmalloc is a malloc wrapper with some tests (like no more memory)
snprintf (file_with_path, (size_t)stringlength, "%s/%s", cur_dir, ep->d_name);
Run Code Online (Sandbox Code Playgroud)

但是如果一个字符串包含一个两字节的utf8字符怎么办?你是如何处理这个问题的?

stringlength*2?
Run Code Online (Sandbox Code Playgroud)

谢谢

sth*_*sth 8

strlen()计算字符串中的字节数,它不关心包含的字节是否表示UTF-8编码的Unicode字符.因此,例如,strlen()包含UTF-8编码"aöü" 5的字符串将返回,因为字符串被编码为"a\xc3\xb6\xc3\xbc".