0

Essentially I have a file that goes like this

Number of entries
Id
    Name of entry
    Float of entry
    Series of numbers
Id
    Name of entry
    Float of entry
    Series of numbers

For example, a file would look like this

4
0
    A. com
    0. 0
    1 2 3
1
    B. com
    0. 0
    0 3
2
    C. com
    0. 0
    0 1
3
    D. com
    0. 0
    1

Essentially what I want to do is to

  • extract the first number, which tells me how many entries there will be.
  • For that many times, extract the following
  • Extract a string
  • Extract a double
  • Extract a series of numbers

I'm ok with the first few, but it's the last part that stumps me. I don't know how many numbers will be below the float. It could be one, two, or a hundred. Here is the code I've written:

ifstream ifile(filename);
    if(ifile.fail()){
        return false;
    }

    ifile >> num_of_webpages;

    for(int i=0; i < num_of_webpages; i++){
        Page temp;
        ifile >> pageid;
        ifile >> page_URL;
        ifile >> pagerank;


        string myline;
        getline(ifile, myline);
        stringstream ss(myline);

        while(ss >> outgoing_link){
            temp.add_url_to_links(outgoing_link);
            cout << outgoing_link << endl;
        }
    }

I can't extract it line by line because the types I'm extracting aren't all the same. If I'm already extracting info from a file, how do I extract the next line, then use that to extract the numbers until the end of the next line?

1 Answers1

0

So the mistake is here

    ifile >> pagerank;

    string myline;
    getline(ifile, myline);
    stringstream ss(myline);

You are assuming that after reading the pagerank that you are on the next line, but you aren't. So your getline will just read the rest of the line containing the pagerank, which has no numbers at all.

Adding an extra getline should solve the problem

    ifile >> pagerank;

    string myline;
    getline(ifile, myline); // move to next line
    getline(ifile, myline);
    stringstream ss(myline);

So would using ignore

    #include <limits>

    ifile >> pagerank;
    ifile.ignore(numeric_limits<streamsize>::max(), '\n'); // move to next line

    string myline;
    getline(ifile, myline);
    stringstream ss(myline);

See here for more info

john
  • 71,156
  • 4
  • 49
  • 68