gpt4 book ai didi

c - 为什么 mbtowc 没有按预期计算字符集?

转载 作者:IT王子 更新时间:2023-10-29 00:51:58 25 4
gpt4 key购买 nike

我想计算文件中的字符(在各种字符集中),我正在使用函数“mbtowc”来检测字符。我无法弄清楚为什么字符和结果值不同。这是我的例子:

char buf[BUFFER_SIZE + MB_LEN_MAX];

int fd = open ("chinese_test", O_RDONLY);

unsigned int bytes, chars;

int bytes_read;

bytes = chars = 0;

while((bytes_read = read(fd, buf, BUFFER_SIZE)) > 0) {
wchar_t wc_buf[BUFFER_SIZE], *wcp;
char *p;
int n = 0;

bytes += bytes_read;

p = buf;
wcp = wc_buf;

while((n = mbtowc(wcp, p, MB_LEN_MAX)) > 0) {
p += n;
wcp++;

chars++;
}

}

printf("chars: %d\tbytes: %d\n", chars, bytes);

我用一些 GB2312 字符的文本测试函数,但是 charsbytes 的值差异太大。

我的测试返回 -> chars: 4638 |字节:17473但是 'wc' linux 命令返回:chars: 16770 |字节:17473

为什么会有这种差异?我做错了什么?


现在我有了这段代码,但结果仍然存在差异。

char buf[BUFFER_SIZE * MB_LEN_MAX];

int fd = open ("test_chinese", O_RDONLY), filled = 0;

unsigned int bytes, chars;

int bytes_read;

bytes = chars = 0;

while((bytes_read = read(fd, buf, BUFFER_SIZE)) > 0) {
wchar_t wc_buf[BUFFER_SIZE], *wcp;
char *p;
int n = 0;

bytes += bytes_read;

p = buf;
wcp = wc_buf;



while(bytes_read > 0) {
n = mbtowc(NULL, p, MB_LEN_MAX);

if (n <= 0) {
p++;
bytes_read--;
continue;
}
p += n;

bytes_read -= n;

chars++;
}

}

printf("\n\nchars: %d\tbytes: %d\n", chars, bytes);

最佳答案

问题是您的BUFFER_SIZEchinese_test 的文件大小和wchar_t 的字节对齐的组合。作为证据,请尝试大幅增加 BUFFER_SIZE - 您应该开始获得所需的答案。

发生的情况是您的程序会处理它接收到的第一个文本 block 。但是想想如果一个字符在第一个和第二个 block 之间拆分,代码中会发生什么,如下所示:

  | First Block                 | Second Block      |
| [wchar_t] [wchar_t] ... [wchar_t] [wchar_t] ... |
| [1,2,3,4] [1,2,3,4] ... [1,2,3,4] [1,2,3,4] ... |

您的代码将从第一个字符的第三个字节开始第二个 block ,并且该字符不会被识别为有效字符。由于 mbtowc 在未找到有效字符时将返回 -1,因此您的循环将立即结束并将整个 block 的字符计数为零。这同样适用于以下 block 。

编辑:
我注意到的另一个问题是您需要设置语言环境才能使 mbtowc 正常工作。考虑到所有这些问题,我编写了以下代码,它为我返回了与 wc 相同的字符数:

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

int BUFFER_SIZE = 1024;
const char *DEFAULT_F_IN = "chinese_test";

struct counts {
int bytes;
int chars;
};

int count_block(struct counts *c, char *buf, int buf_size)
{
int offset = 0;
while (offset < buf_size) {
int n = mbtowc(NULL, buf + offset, MB_CUR_MAX);
if (n <= 0) {
break;
}

offset += n;
c->bytes += n;
c->chars++;
}

return buf_size - offset;
}

void get_counts(struct counts *c, FILE *fd)
{
char buf[BUFFER_SIZE];
c->bytes = 0;
c->chars = 0;

int bytes_read;
while((bytes_read = fread(buf, sizeof(*buf), BUFFER_SIZE, fd)) > 0) {
int remaining = count_block(c, buf, bytes_read);
if (remaining == 0) {
continue;
} else if (remaining < MB_CUR_MAX) {
fseek(fd, -remaining, SEEK_CUR);
} else {
perror("Error");
exit(1);
}
}
}

int main(int argc, char *argv[]) {
FILE *fd;
if (argc > 1) {
fd = fopen(argv[1], "rb");
} else {
fd = fopen(DEFAULT_F_IN, "rb");
}

setlocale(LC_ALL, "");
struct counts c;
get_counts(&c, fd);
printf("chars: %d\tbytes: %d\n", c.chars, c.bytes);

return 0;
}

关于c - 为什么 mbtowc 没有按预期计算字符集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9221436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com