2016-12-23 54 views
0

我做了我的簡單txt掃描器,他將文本寫入與我的選擇匹配的文件中。問題是寫入文件時,而不是筆寫入,例如,洀漀。在照片中可以看到,例如:將Unicode字符串寫入一個txt文件

enter image description here

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() 
{ 
int offset; 
wstring DBSearchLine, ScanLine; 

wifstream ScanFile, DBSearchFile; 
wofstream ResultFile; 
ScanFile.open("ScanFile.txt", ios_base::binary); 
ResultFile.open("ResultFile.txt", ios::out, ios_base::binary); 

if (ScanFile.is_open()) 
{ 
    while (!ScanFile.eof()) 
    { 
     DBSearchFile.open("DBSearchFile.txt", ios_base::binary); 
     if (!DBSearchFile.is_open()) 
     { 
      cout << "Error open DBSearchFile.txt" << "\n"; 
      break; 
     } 

     getline(ScanFile, ScanLine); 
     wcout << "Scan line is - " << ScanLine << "\n"; 

     while (!DBSearchFile.eof()) 
     { 
      getline(DBSearchFile, DBSearchLine); 
      wcout << "DBSearchLine is -" << DBSearchLine << "\n"; 
      if ((offset = ScanLine.find(DBSearchLine, 0)) != string::npos) 
      { 
       ResultFile << ScanLine << L"\n"; 
      } 
     } 
     DBSearchFile.close(); 
    } 
    ScanFile.close(); 
} 
else 
{ 
    cout << "Error open ScanFile.txt" << "\n"; 
} 
system("PAUSE"); 
return 0; 
} 
+0

請問,如果你使用字符,而不是寬字符工作的呢? – ZDF

+0

@ZDF如果你的意思是字符串是無法使用getline的wstring,因爲插入的字符串是wstring。並且在編碼方面應該有所不同 – Marek

+0

'ResultFile.open(「ResultFile.txt」,ios :: out,ios_base :: binary)' - 不應該是'ios :: out |的ios_base :: binary'?此外,不能再現 - 鏗鏘3.9.0,Ubuntu 14.04.05 x86_64。 –

回答

0
#include <iostream> 
#include <fstream> 
#include <string> 
#include <locale> 
#include <codecvt> 

using namespace std; 

int main() 
{ 
    /* via http://stackoverflow.com/a/5105192/4005233 
     changes the encoding of the console and all subsequently opened 
     files */ 
    std::locale::global(std::locale("")); 

    wifstream ScanFile; 
    ScanFile.open("ScanFile.txt", ios_base::binary); 
    if (!ScanFile.is_open()) { 
     cout << "Error open ScanFile.txt" << "\n"; 
     return 1; 
    } 

    wofstream ResultFile("ResultFile.txt", ios::out); 

    while (!ScanFile.eof()) 
    { 
     wifstream DBSearchFile; 
     DBSearchFile.open("DBSearchFile.txt", ios_base::binary); 
     if (!DBSearchFile.is_open()) 
     { 
      cout << "Error open DBSearchFile.txt" << "\n"; 
      break; 
     } 

     wstring ScanLine; 
     getline(ScanFile, ScanLine); 
     wcout << "Scan line is - " << ScanLine << "\n"; 

     do 
     { 
      wstring DBSearchLine; 
      getline(DBSearchFile, DBSearchLine); 
      // have all lines been read? 
      if(!DBSearchLine.length()) 
       break; 
      wcout << "DBSearchLine is -" << DBSearchLine << "\n"; 

      if (ScanLine.find(DBSearchLine, 0) != string::npos) 
      { 
       ResultFile << ScanLine << L"\n"; 
       break; // found a match, no need to search further 
      } 
     }while(1); 
     DBSearchFile.close(); 
    } 

    ScanFile.close(); 

    return 0; 
} 

這是使用有和沒有BOM文件進行測試。

最內層的循環必須改變,以處理結尾帶有換行符的文件;如果我沒有這樣做,它會匹配一個總是爲真的空字符串。

(我也改變了一些其他的事情,根據我的編碼風格,最重要的變化是一個正確的頂部)

相關問題