gpt4 book ai didi

C++ 文本文件,汉字

转载 作者:行者123 更新时间:2023-11-28 01:08:52 27 4
gpt4 key购买 nike

我有一个 C++ 项目应该添加 <item>到每一行的开头和</item >到每一行的末尾。这适用于普通英文文本,但我有一个中文文本文件,我想这样做,但它不起作用。我通常使用 .txt 文件,但为此我必须使用 .rtf 来保存中文文本。在我运行我的代码后,它变成了乱码。这是一个例子。

{\rtf1\adeflang1025\ansi\ansicpg1252\uc1\adeff31507\deff0\stshfdbch31506\stshfloch31506\stshfhich31506\stshfbi31507\deflang1033\deflangfe1033\themelang1033\themelangfe0\themelangcs0{\fonttbl{\f2\fbidi \fmodern\fcharset0\fprq1{*\panose 02070309020205020404}Courier New;}

代码:

int main()
{
ifstream in;
ofstream out;
string lineT, newlineT;

in.open("rawquote.rtf");
if(in.fail())
exit(1);
out.open("itemisedQuote.rtf");
do
{
getline(in,lineT,'\n');
newlineT += "<item>";
newlineT += lineT;
newlineT += "</item>";
if (lineT.length() >5)
{
out<<newlineT<<'\n';
}
newlineT = "";
lineT = "";
} while(!in.eof());
return 0;
}

最佳答案

看起来像 RTF ,这是有道理的,因为你说这是一个 rtf 文件。

基本上,如果您在打开时转储该文件,您会看到它看起来像这样...

另外,你应该重新审视你的循环

std::string line;
while(getline(in, line, '\n'))
{
// do stuff here, the above check correctly that you have indeed read in a line!
out << "<item>" << line << "</item>" << endl;
}

关于C++ 文本文件,汉字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4617115/

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