0

我一直在努力让这个工作。该程序应该采用两个参数,一个用于缓冲区大小,另一个用于文件名,并将该文件从 UTF-32 转换为 UTF-8。我一直在使用 fgetc() 函数用 Unicode 代码点填充一个 int 数组。我已经测试 printint 输出缓冲区的内容,并且它具有所有这些空字符而不是每个代码点。

例如,对于仅包含字符“A”的文件:缓冲区 [0] 为 0 缓冲区 [1] 为 0 缓冲区 [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;
    }
  }
}
4

1 回答 1

2

fgetc() 返回一个字节,而不是 unicode 代码点。

从那里开始,基于那个错误的假设,整个事情都垮了。

于 2011-04-10T01:29:29.030 回答