0

I need to split up a line coming in from a file. The file contains a list of books with an id, Author, published date, and title.

91507 Lewis Carroll 1865 Alice in Wonderland

The author name I can just split up into first and last, but what about the title?? I have no idea how to deal with that. Should I just create a second file so I can use getline?

I am a newb and I don't understand how to use stringsteam for this, or split() or anything like that, so if that is what you suggest could you please explain what's happening? I haven't understood any of the examples I've looked at well enough to modify them to suit my purpose.

Katie
  • 21
  • 1
  • 7

2 Answers2

1
ifstream fin;
fin.open(filename)
string id;
string first;
string last;
string year;
string title;
if(fin.is_open())
{
    while(!fin.eof())
    {
      fin >> id >> first >> last >> year;
      getline(fin, title);
      cout << first << " " << last << " " << title << endl;
    }
   fin.close();
}
Manvir
  • 769
  • 1
  • 7
  • 15
  • Could you please explain while(!fin.eof())? I need to go try it in my program and then I will mark as answered, but I usually say while (fin >> id >> first >> last >> year >> title ); or something of the sort. When the file reaches the end the while loop fails and it stops, is that right? So how does .eof() work? is it a built in function? – Katie Apr 16 '17 at 21:27
  • while(!fin.eof()) eof- stands for end of file. !fin.eof() = true because eof() returns false if eofbit flag is not set. When the the loop tries to go past the end of file the eofbit flag is set eof() returns true when the the flag is set – Manvir Apr 16 '17 at 22:01
  • Your method works too. I just got accustomed to using !fin.eof(). eof() is a public member function for the library inherited from ios (http://www.cplusplus.com/reference/fstream/ifstream/) – Manvir Apr 16 '17 at 22:42
  • Awesome! Can't believe it's so simple after all my struggling! Just one more question- why do you use the if statement? if(fin.is_open()) – Katie Apr 16 '17 at 23:06
  • I do it because what if the file doesn't open successfully and if you don't have is_open then you'll have more errors. And u have to do this for ofstream too. Don't forget to .close() it too because if you leave the file open then the file might be corrupted. – Manvir Apr 16 '17 at 23:33
  • 1
    @KLG52486 So i learned something new it is better to use `while(fin >> id >> first >> last >> year >> title)` sorry even though .eof() worked in this case it is bad to use .eof() [link](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Manvir May 08 '17 at 06:46
0

Do this:

#include <fstream>
using namespace std;

int main()
{
    ifstream fin("somefile.txt");
    string id = "";
    string authFirst = "";
    string authLast = "";
    string date = "";
    string title = "";
    string temp = "";
    fin >> id >> authFirst >> authLast >> date >> title;
    getline(fin, temp);

    title += temp;

    cout << title << endl;
    return 0;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Henri
  • 145
  • 13
  • After looking at my post I realize that you don't even need to input title at the end of the fin series; you can just let the getline function on the next line take care of the title for you. – Henri Apr 16 '17 at 04:14