简体   繁体   中英

Read a text file using ifstream

I'm trying to read a textfile that contain:-

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

The 0 is the id and the http...country b is keywords, but my code keeps on reading the 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;
        }
    }
}

Can anyone help me with this problem? I don't know what I'm doing wrong.

As @SamVarshavchik stated in comments, there are multiple issues with your code:

Pretty much everything is wrong,here. while (.fin.eof()) is always a bug. >> reads a single whitespace-delimited word, you're expecting it to read all the remaining words on the line. >> does not work this way. end is used before it is uninitialized. This is undefined behavior.

Try something more like this instead:

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

Alternatively, you can simplify the inner loop by using std::istringstream instead, eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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