简体   繁体   中英

Problem reading a tab delimited file in C++

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

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)
*/

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