2017-06-05 87 views
2

我完全是编程新手,我通过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

+0

作为一个便笺,我想我在Visual Studio 2017中发现了一个错误。当我选择Log(1000)/ Log(10)时,如果全部是整数,我会得到2。如果都是双倍的,我会得到3.0。如果我尝试向2的int值添加2,我得到5. 我向Microsoft报告了这一情况。 – HappyS5

+0

没有文字,但编码文本。当你写一个文件时,你选择了字符编码。读者只需要使用它,这意味着你必须告诉他们。这一切都始于您的源代码,其中,作为文本,您有编辑器使用的编码。然后你必须告诉你的编译器“[源字符集](https://docs.microsoft.com/en-us/cpp/build/reference/source-charset-set-source-character-set)”是什么。 (很有可能,这些默认值在您不知情的情况下匹配。)重复每次写入文本然后读取。提示:/ execution-charset是下一个。 –

回答

-1

我用GCC complie你的代码在Linux中,存在一些误区。

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 problem.Changing \P\nP通过使用fstream。我代码校正第二one.As到第一个正确的是在下面(仅提供第一17行,别人等于你的)

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   } 

这里是输出的一部分。

1 i: 900 3 
    2 Number 3: 9 
    3 Number 2: 0 
    4 Number 1: 0 
    5 i: 901 3 
    6 Number 3: 9 
    7 Number 2: 0 
    8 Number 1: 1 
    9 i: 902 3 
10 Number 3: 9 
11 Number 2: 0 
12 Number 1: 2 
13 i: 903 3 
14 Number 3: 9 
15 Number 2: 0 
16 Number 1: 3 
17 i: 904 3 
18 Number 3: 9 
19 Number 2: 0 
20 Number 1: 4 
21 i: 905 3 
22 Number 3: 9 
23 Number 2: 0 
24 Number 1: 5 
25 i: 906 3 
26 Number 3: 9 
27 Number 2: 0 
28 Number 1: 6 
29 i: 907 3 
30 Number 3: 9 
31 Number 2: 0 
32 Number 1: 7 
+0

是的,我在控制台上得到输出,但输出到文本文件是亚洲字符。 – HappyS5

+0

这不是OP的问题的答案。见[回答]。 –

0

看起来您已经发现记事本的编码检测猜测错误的情况。

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

+0

哈!只有我应该猜测。无论如何,我不知道如何才能做出正确的决定。之前,我得到它输出正确的格式,但这是与一个不同的switch语句。在那种情况下,尽管我还有其他的(i == 9),但每个数字后面都会有9个。这是一个不同的故事。 – HappyS5

+0

我只能通过重复字符串“Nine”或“NineHundredand”而没有换行符或空格来猜出错误。这看起来确实是一个角落案例。 –

+0

我也尝试了输出之间的单词之间的空间,并获得亚洲字符。 – HappyS5

相关问题