使用c ++中的getline()从文本文件中读取字符串

时间:2015-12-30 14:40:21

标签: c++ visual-studio-2010 file getline

我正在研究在两个单独的文本(.txt)文件中查找类似字符串数量的简单问题,这些文件名为“4_1.txt”和“4_2.txt”。但是我的代码中的getline()函数不会读取文件,因为它返回文件流'00000000'(我已经使用'cout'检查了它)。使用ifstream.open()函数已成功打开两个文本文件。为什么getline()函数不起作用,以及它的调试需要什么? (我使用的是Visual C ++ 2010)。代码是 -

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


using namespace std;

int main ()
{
    int match = 0,individual_match=0;
    vector<int> arr_match;

    string file_name_1 = "Database/4_1.txt";
    string file_name_2 = "Database/4_2.txt";
    string s1,s2;

    ifstream ifs1(file_name_1);
    if (!ifs1)
    {
        cout<<"File 1 read error :"<<endl;
        exit(0);
    }

    ifs1.open(file_name_1, ios::in);
    cout<<"File No. "<<file_name_1<<" succesfully opened"<<endl;
    ifstream ifs2(file_name_2);
    if (!ifs2)
    {
        std::cout<<"File 2 read error :"<<endl;
        exit(0);
    }
    ifs2.open(file_name_2, ios::in);
    cout<<"File No. "<<file_name_2<<" succesfully opened"<<endl;


    // Matching Begins
    cout<<"Matching Between: "<<file_name_1<<" and "<<file_name_2<<endl;
    while (getline(ifs1,s1))
    {
        while (getline(ifs2,s2))
        {
            if (strcmp(s1.c_str(),s2.c_str()) == 0)
                match++;          
        }
        ifs2.clear();
        ifs2.seekg(0,ios::beg);

    }
    cout<<"No. of Matching Minutiae between "<<file_name_1<<" and "<<file_name_2<<" is : "<<match<<endl;
    arr_match.push_back(match);
    ifs2.close();

    cout<<"Printing the contents of the Vector: "<<endl;

    for (std::vector<int>::iterator i = arr_match.begin(); i != arr_match.end(); ++i)
        cout << *i << ' ';

    system("pause");
    return (0);
}

下面给出“4_1.txt”和“4_2.txt”的内容。两者都包含20个字符串(每个字符串包含64个HEX字符)。

4_1.txt

f789909693b3898605450349b35d0218bc556c51f960f944e50ae242dc3446a3 b780f20d75afc6afe44ccc270ba7e499c070a95e6535bcc71de8aa36ced3b5b6 12269c796b58fb51020e17da59733d060552b37f7916e2f97df9f9bdc0e235a0 94ca7771bcbfb3a51a6feb57e67955bf57e7655c4e38f23c42f2cb8f26bed543 290a0b92873bdf4e47986dc5208037bad7527653bff700dc53c1e57eb98103c1 5b7267c26cc279fdbe97b434c889c0ec3831f2294b19e4b8a9228de154551503 8a835b9bf9c1f8976256bf02427710f8ecf85f55aa59e5084421be8793752362 3b37ef6a1be31e06efd2f3cfa7e70d2b25d95efec97e5b71503a95e249d13250 c6fab63609e8a0b0dad162a533ce5da4d8f4d40b652c8af06c6a542f4eba74b5 61aad04aa9341a27da25994e4e72be78828a848291b781d8c19a5192b860f033 92b18972560ad2bbd2800ccd28c975279ef95f74abb8cf37298def1cb681af9e 900f7b407b5aa1f1759d248ae30e80a9e9f6d835d5d630c57330271264edc4ab 4a081bff0bf93d06ee54ff45353e8b65bce23522ae6a45769f5968ff5956356e 058b346de9b51add4eca9aec2ab0ed11aed227590421e2a05070ae1236954094 be1dfca7b57921df356fe39227da629c8ce9d04ea37b7dc109639a2cd99b127a 6ef3dcb7be11f8c49ebcf27b2bd215629cebe39577cd7a29e6005ffffb4a883d 9e3b6232cb7bd60ef9c5088e1c6dac834ddc05d916e88e2aacf84bb18c8d707a ef13ebc3a57a1b51c17b45fcfef8eef761deab8371cc9e4eca14962d5763123d 4459b2909cef1e99ddfb4be233208753a4ed2f43343df6d5b4561c5e9ead51c9 059abcdaf2f2d2fbd6d40a18ff9e7d23db83e8ea1662b1b8d96f7ff3a189dbd0

4_2.txt

be1dfca7b57921df356fe39227da629c8ce9d04ea37b7dc109639a2cd99b127a 6ef3dcb7be11f8c49ebcf27b2bd215629cebe39577cd7a29e6005ffffb4a883d 1788c74b1c9262866c2071b65df7bfcb7911c2b064c931b580515c2d9d2cd7f8 ef13ebc3a57a1b51c17b45fcfef8eef761deab8371cc9e4eca14962d5763123d ca35d6668c8ef049df346e4fb2c18f50dac07bdf0b8d96a493c4df2b4143fe34 07915fe7c4d1b930ab31e4807dcaa0b2f5ce124030ce6d365283fc38fce478cd 4a081bff0bf93d06ee54ff45353e8b65bce23522ae6a45769f5968ff5956356e d65112fe4b7673c7e4677845ab91b4a6686e4543ea7b1c2420f89481f7d1b965 9ae8f17cfc8ba7fd8fb34b2a194ef965a3b36a40839a46eeab1350e916692ac9 e94e063888d213e0839a3c52bb8c0c1c9b9d4dbf67e5a3420a27b0efdf551853 567c38161a125f1d049703d0e67e1062fdd9b975ecf00688cc61415c997e71c6 143cf2c38c63401ee6c6d4514733c832f013c5647b03c92edae973e7a1c440a7 59279341ea59fbf34025024596b670b6df2c9f80e71b9ad19aea71ba43b083fc ccd5e4c20eae9e4ada72b6a93cbf14bff6e15d0a525c7b01e74a2eb0edfbc510 a89d6642c31c33525be583f4333b37d260d14903319a846e4f1e9e091a978592 a5ca6c0335a3860b31a64f43bf3021207ac91133c82bbc1736ab4f4e2922afed e32df00f65acc1db0a8264012e7074dbc58bd6f47b8c52004e0ab59244740cd5 68fb74e20a2f319724f156afdb0a5f9470feb0bcbacfa1f2ca998fa86adda627 f789909693b3898605450349b35d0218bc556c51f960f944e50ae242dc3446a3 4459b2909cef1e99ddfb4be233208753a4ed2f43343df6d5b4561c5e9ead51c9

0 个答案:

没有答案