我需要将未编码的字符串转换为其适当的语言。我需要逐行读取文本文件。一行可能包含unicode之类的东西


  \ xE6 \ xAC \ xA2 \ xE8 \ xBF \ x8E


这基本上是中文文本,等于


  欢迎


现在,我需要从文本文件中删除此行(\ xE6 \ xAC \ xA2 \ xE8 \ xBF \ x8E),将此unicode转换为中文文本,然后将此中文文本附加到文本文件中。

以下是我的data.txt文件的内容:

testing
programming
\xE6\xAC\xA2\xE8\xBF\x8E
development


我想得到的文件内容为:

testing
programming
development
欢迎


以下是我到目前为止所做的

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


#define MAX 256

  int main()
  {
        int ctr = 0;
        char ch;
        FILE *fptr1, *fptr2;
        char fname[MAX] = "data.txt";
        char str[MAX], temp[] = "temp.txt";
        char str2[256];

        fptr1 = fopen(fname, "r");
        if (!fptr1)
        {
                printf(" File not found or unable to open the input file!!\n");
                return 0;
        }
        fptr2 = fopen(temp, "w"); // open the temporary file in write mode
        if (!fptr2)
        {
                printf("Unable to open a temporary file to write!!\n");
                fclose(fptr1);
                return 0;
        }

        // copy all contents to the temporary file except the specific line with unicode characters
        while (!feof(fptr1))
        {
            strcpy(str, "\0");
            fgets(str, MAX, fptr1);
            if (!feof(fptr1))
            {
                ctr++;
                if(strstr(str,"\\")!=NULL)
                {
                    memset(str2,'\0',sizeof(str2));
                    printf("Input String Contains Unicode Character\n");
                    str[strlen(str)-1]='\0';

                    sprintf(str2,"echo %s >> data.txt",str);
                    printf("Final String: %s\nUnicode String Size: %ld\n",str2,strlen(str));
                    system(str2);
                }
                else
                {

                    fprintf(fptr2, "%s", str);
                }
            }
        }
        fclose(fptr1);
        fclose(fptr2);
        remove(fname);          // remove the original file
        rename(temp, fname);    // rename the temporary file to original name
/*------ Read the file ----------------*/
   fptr1=fopen(fname,"r");
            ch=fgetc(fptr1);
          printf(" Now the content of the file %s is : \n",fname);
          while(ch!=EOF)
            {
                printf("%c",ch);
                 ch=fgetc(fptr1);
            }
        fclose(fptr1);
/*------- End of reading ---------------*/
        return 0;

  }


当尝试编译并运行此代码时,以下是我看到的输出

Input String Contains Unicode Character
Final String: echo \xE6\xAC\xA2\xE8\xBF\x8E >> data.txt
Unicode String Size: 24
 Now the content of the file data.txt is :
testing
programming
development
xE6xACxA2xE8xBFx8E


更改下面几行时,相同的代码可以正常工作

 sprintf(str2,"echo %s >> data.txt",str);
 sprintf(str2,"echo %s >> data.txt","\xE6\xAC\xA2\xE8\xBF\x8E");


但是,当从文件中读取值时,它不起作用。

也是这一行,该字符串被标识为具有正确大小的unicode字符串

printf("Final String: %s\nUnicode String Size: %ld\n",str2,strlen(str));
The String Size: 6


能否请一个人告诉我,从文本文件中读取时如何将值转换为中文。

最佳答案

您必须确定行中的\x位置,说出指针p然后指向下一个字符。现在

char hex[3] = { p[0], p[1], 0 };
char val = strtoul(hex, 0, 16);
p += 2;


将返回在val中用十六进制解释的以下两个字节的值。

08-18 09:58
查看更多