使用C ++获取亚洲字符的文本文件

时间:2017-06-05 02:01:07

标签: c++ output character

我是编程新手,我正在通过Bronson博士的书自学C ++。

我查看了其他修正案,但我不理解它们。

我正在尝试将数字转换为短语值的项目。 1 =一,二=两,等等。我不能在一到二之间有空格,所以我输出没有空格。当我打开文本文件时,文件中只有亚洲字符。作为一个新手,我首先担心的是我被黑了。无论如何,这是我的代码。

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

2 个答案:

答案 0 :(得分:0)

看起来你发现了一个角落案例,其中记事本的编码检测错误。

使用ASCII编码的字符串"NineHundredand"给出字节序列0x4E 0x69 0x6E 0x48 0x75 0x6E 0x64 0x72 0x65 0x64 0x61 0x6E 0x64。事实证明,相同的字节序列也是字符串"楎敮畈摮敲慤摮"的有效UTF-16编码。因为没有什么可以告诉记事本哪些字节的解释是正确的,所以必须猜测。它如何使它做的猜测可能非常复杂,可能没有记录在任何地方(至少不公开),但在这种情况下它猜错了。奇怪的是,它似乎取决于你重复短语"NineHundredand"多少次。少于15次,它会猜测编码为ASCII;更重要的是,它决定使用UTF-16编码文件。

答案 1 :(得分:-1)

我在linux中用gcc编译你的代码,有一些错误。

as.cpp: In function ‘int main()’:
as.cpp:9: error: aggregate ‘std::ofstream outFile’ has incomplete type and cannot be defined
as.cpp:15: error: unknown escape sequence '\P'
as.cpp:23: error: ‘log’ was not declared in this scope

我添加了math.h来纠正log问题。将\P更改为\nP以更正第二个问题。至于使用{{1}更正的第一个问题。我的代码在下面。(只提供前17行,其他等于你的)

fstream

这是输出的一部分。

1 #include<iostream>
  2 #include<fstream>
  3 #include<math.h>
  4 using namespace std;
  5 int main()
  6 {
  7         int i;
  8         int length, mod, number1, number2,number3,numbera,numberb;
  9         string  str1;
 10         string filename = "C:\\Users\\miram\\OneDrive\\Documents\\Visual Studio 2017\\CPlus    PlusProjects\\Project Euler\\Files\\17.WordSum1.txt";
 11         std::ofstream outFile;
 12         outFile.open(filename.c_str());
 13         if (outFile.fail())
 14         {
 15                 cout << "\nThe file named " << filename << " did not successfully open."
 16                         << "\nPlease check to make sure the file exists.";
 17         }