我一直在努力让这个工作。该程序应该采用两个参数,一个用于缓冲区大小,另一个用于文件名,并将该文件从UTF-32转换为UTF-8。我一直在使用fgetc()函数用Unicode代码点填充int数组。我已经测试了printint我的缓冲区的内容,它有所有这些空字符而不是每个代码点。
例如,对于仅包含字符“A”的文件: buffer [0]为0 buffer [1]为0 buffer [2]为0 缓冲区[3]是41
U + 7F以上任何东西的代码点最终会分开。
以下是初始化缓冲区的代码:
int main(int argc, char** argv) {
if (argc != 3) {
printf("Must input a buffer size and a file name :D");
return 0;
}
FILE* input = fopen(argv[2], "r");
if (!input) {
printf("The file %s does not exist.", argv[1]);
return 0;
} else {
int bufferLimit = atoi(argv[1]);
int buffer[bufferLimit];
int charReplaced = 0;
int fileEndReached = 0;
int i = 0;
int j = 0;
while(1) {
// fill the buffer with the characters from the file.
for(i = 0; i < bufferLimit; i++){
buffer[i] = fgetc(input);
// if EOF reached, move onto next step and mark that
// it has finished.
if (buffer[i] == EOF) {
fileEndReached = 1;
break;
}
}
// output buffer of chars until EOF or end of buffer
for(j = 0; j <= i; j++) {
if(buffer[j] == EOF) {
break;
}
// check for Character Replacements
charReplaced += !convert(buffer[j]);
}
if(fileEndReached != 0) {
break;
}
}
//return a 1 if any Character Replacements were used
if(charReplaced != 0) {
return 1;
}
}
}
答案 0 :(得分:2)
fgetc()返回一个字节,而不是unicode代码点。
从那时起基于这种错误的假设,整个事情就会崩溃。