我完全是编程新手,我通过Dr. Bronson的书教自己C++。使用C++获取亚洲字符的文本文件
我看过其他修复程序,但我不理解它们。
我想做一个项目,我将数字转换为他们的短语值。 1 = 1,2 = 2等。我不能在1和2之间有空格,所以我输出时没有空格。当我打开文本文件时,文件中只有亚洲字符。作为新手,我首先担心的是我遭到黑客入侵。无论如何,这是我的代码。
int main()
{
int i;
int length, mod, number1, number2,number3,numbera,numberb;
string str1;
string filename = "C:\\Users\\miram\\OneDrive\\Documents\\Visual Studio 2017\\CPlusPlusProjects\\Project Euler\\Files\\17.WordSum1.txt";
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "\nThe file named " << filename << " did not successfully open."
<< "\Please check to make sure the file exists.";
}
for (i = 900; i <= 999; i++)
{
if (i <= 999)
{
length = (log(i)/log(10)) + 1;
cout << "i: " << i << " " << length << endl;
}
else if (i == 1000)
{
length = 4;
cout << "i: " << i << " " << length << endl;
}
if (length == 3)
{
number1 = i % 10;
numbera = i/10;
number2 = numbera % 10;
numberb = i/100;
number3 = numberb;
cout << "Number 3: " << number3 << endl;
cout << "Number 2: " << number2 << endl;
cout << "Number 1: " << number1 << endl;
}
switch (length)
{
case 4:
{
outFile << "OneThousand";
break;
}
case 3:
{
if (number3 == 1)
{
outFile << "OneHundredand";
}
else if (number3 == 2)
{
outFile << "TwoHundredand";
}
else if (number3 == 3)
{
outFile << "ThreeHundredand";
}
else if (number3 == 4)
{
outFile << "FourHundredand";
}
else if (number3 == 5)
{
outFile << "FiveHundredand";
}
else if (number3 == 6)
{
outFile << "SixHundredand";
}
else if (number3 == 7)
{
outFile << "SevenHundredand";
}
else if (number3 == 8)
{
outFile << "EightHundredand";
}
else if (number3 == 9);
{
outFile << "NineHundredand";
}
break;
}
}
}
return 0;
}
Text file with Asian characters
作为一个便笺,我想我在Visual Studio 2017中发现了一个错误。当我选择Log(1000)/ Log(10)时,如果全部是整数,我会得到2。如果都是双倍的,我会得到3.0。如果我尝试向2的int值添加2,我得到5. 我向Microsoft报告了这一情况。 – HappyS5
没有文字,但编码文本。当你写一个文件时,你选择了字符编码。读者只需要使用它,这意味着你必须告诉他们。这一切都始于您的源代码,其中,作为文本,您有编辑器使用的编码。然后你必须告诉你的编译器“[源字符集](https://docs.microsoft.com/en-us/cpp/build/reference/source-charset-set-source-character-set)”是什么。 (很有可能,这些默认值在您不知情的情况下匹配。)重复每次写入文本然后读取。提示:/ execution-charset是下一个。 –