0

Right now I am trying to read in a list of books that have tab separated information and just printing the title. Eventually I will add each piece of info to a vector with their names. When I switched the delimiter to a tab from nothing or a one character space, suddenly nothing was outputted.I've look over stack exchange, but most of these solutions aren't telling me why mine doesn't work. Here is my code

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

int main() {
ifstream DataFile;
string str;
string title;
string author;
string publisher;
string date;
string ficornon;

if(!DataFile)
{
    cout<<"error";

}
DataFile.open("/Users/Kibitz/Desktop/bestsellers.txt",ios::in);
getline(DataFile,title);
while(!DataFile.eof()) // To get you all the lines.
{

    cout<<title<<endl;
    getline(DataFile,author);
    getline(DataFile,publisher);
    getline(DataFile,date);
    getline(DataFile,ficornon);
    getline(DataFile,title);
}
DataFile.close();
return 0;

}

First two lines of input file:

1876    Gore Vidal    Random House    4/11/1976    Fiction
23337    Stephen King    Scribner    11/27/2011    Fiction
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Show your `bestsellers.txt`. – Swordfish Sep 27 '18 at 20:05
  • Use `'\t'` as the `delim` parameter of [getline()](https://en.cppreference.com/w/cpp/io/basic_istream/getline) – Ripi2 Sep 27 '18 at 20:07
  • I've tried using \t as the delim, but then nothing is outputted this code works when \t is not the delim like above. – Jake Barcelona Sep 27 '18 at 20:10
  • Please edit your post with a text sample of the input file. – Thomas Matthews Sep 27 '18 at 20:11
  • The post was edited – Jake Barcelona Sep 27 '18 at 20:16
  • You have to be careful with the phrase "The code works." Few people ask question here when the code works. If `getline` isn't working as it should with tab as the delimiter, show the code that doesn't work. You'll get better results. – user4581301 Sep 27 '18 at 20:16
  • How sure are you you really have tabs in the file? Some editors kindly replace tabs with spaces. – user4581301 Sep 27 '18 at 20:18
  • You're right the file had extra space, didn't even think of that. Thank y'all for your help. – Jake Barcelona Sep 27 '18 at 20:29

1 Answers1

0

There is a piece of code that reads your file example correctly and print out to stdout. Please notice delimiters used in 'getline' funcion: tab (character '\t') is used to mark end of data fields, and new line character '\n' is used to mark end of line. Check your data file to see it really contains tab delimiters. The 'peek' function checks for next character in stream, and if there is not more chars, it sets 'eof' flag of the stream. Because there could be more conditions that can invalidate stream for reading I use good() function as the condition in 'while' loop.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;   

int main() {
std::ifstream DataFile;
string str;
string title;
string author;
string publisher;
string date;
string ficornon;

  DataFile.open("bestsellers.txt",std::ifstream::in);
// getline(DataFile,title);  // don't need this line
  DataFile.peek(); // try state of stream
  while(DataFile.good())  
  {
    getline(DataFile,str,  '\t');     // you should specify tab as delimiter between filelds
    getline(DataFile,author, '\t');   // IMO, better idea is to use visible character as a delimiter, e.g ','  or ';' 
    getline(DataFile,publisher, '\t');
    getline(DataFile,date,'\t');
    getline(DataFile,ficornon,'\n');   // end of line is specified by '\n'
    std::cout << str << " " << author << " " << publisher <<  " " << date << " " << ficornon << std::endl;
    DataFile.peek(); // set eof flag if end of data is reached
  }

  DataFile.close();
  return 0;
}
/*
Output:
1876 Gore Vidal Random House 4/11/1976 Fiction
23337 Stephen King Scribner 11/27/2011 Fiction
(Compiled and executed on Ubuntu 18.04 LTS)
*/
risbo
  • 188
  • 7