繁体   English   中英

使用 ifstream 读取文本文件

[英]Read a text file using ifstream

我正在尝试读取包含以下内容的文本文件:-

0 http://www.gutenberg.net/dickens/otwist/0.html
the project gutenberg ebook of oliver twist by charles dickens 13 in our series by charles dickens copyright laws are changing all over the world be sure to check the copyright laws for your country b

1 http://www.gutenberg.net/dickens/otwist/1.html
chapter I treats of the place where oliver twist was born and of the circumstances attending his birth among other public buildings in a certain town which for many reasons it will be prudent to refra

0是 ID, http...country b是关键字,但我的代码一直在读取0

BST searchEngine;
string filename;

int main()
{
    ifstream fin("data.txt");

    //cout << "Please Enter a Filename: ";
    //cin >> filename;

    //fin.open("data.txt");

    int ID;
    string Keyword;
    string Keywords;
    string URL;

    while (!fin.eof())
    {
        Keyword = "";
        fin >> ID >> URL >> Keywords;

        cout << "ID: " << ID << endl;

        int start = 0;
        size_t end;

        while (end != string::npos)
        {
            end = Keywords.find(' ', start);
            Keyword = Keywords.substr(start, end - start);
            searchEngine.insert(Keyword, URL);
            start = end + 1;
        }
    }
}

谁能帮我解决这个问题? 我不知道我做错了什么。

正如@SamVarshavchik 在评论中所述,您的代码存在多个问题:

几乎一切都是错的,在这里。 while (.fin.eof())始终是一个错误。 >>读取单个以空格分隔的单词,您希望它读取该行的所有剩余单词。 >>这样不行。 end在未初始化之前使用。 这是未定义的行为。

尝试更像这样的东西:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
...
using namespace std;

int main()
{
    BST searchEngine;
    //string filename;

    //cout << "Please Enter a Filename: ";
    //cin >> filename;

    ifstream fin(/*filename*/"data.txt");

    int ID;
    string Keyword;
    string Keywords;
    string URL;

    while ((fin >> ID >> ws) && getline(fin,URL) && getline(fin,Keywords))
    {
        cout << "ID: " << ID << endl;

        size_t start = 0;
        size_t end;

        do
        {
            end = Keywords.find(' ', start);
            if (end == string::npos)
            {
                Keyword = Keywords.substr(start);
                searchEngine.insert(Keyword, URL);
                break;
            }
            Keyword = Keywords.substr(start, end - start);
            searchEngine.insert(Keyword, URL);
            start = end + 1;
        }
        while (true);
    }
}

或者,您可以改用std::istringstream来简化内部循环,例如:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
...
using namespace std;

int main()
{
    BST searchEngine;
    //string filename;

    //cout << "Please Enter a Filename: ";
    //cin >> filename;

    ifstream fin(/*filename*/"data.txt");

    int ID;
    string Keyword;
    string Keywords;
    string URL;

    while ((fin >> ID >> ws) && getline(fin,URL) && getline(fin,Keywords))
    {
        cout << "ID: " << ID << endl;

        istringstream iss(Keywords);
        while (iss >> Keyword)
        {
            searchEngine.insert(Keyword, URL);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM