0

我制作了一个简单的 txt 扫描仪,它将文本写入与我的选择相匹配的文件中。问题是写入文件而不是笔写入时,例如洀漀。在图片上,您可以看到例如:

在此处输入图像描述

#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;
}
4

1 回答 1

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的文件进行测试的。

必须更改最里面的循环以处理末尾带有换行符的文件;如果我没有这样做,它将与一个始终为真的空字符串匹配。

(我还根据我的编码风格改变了一些其他的东西,重要的改变是顶部的那个)

于 2016-12-23T23:25:40.780 回答