简体   繁体   English

在C ++中读取制表符分隔文件时出现问题

[英]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. 有一段代码可以正确读取您的文件示例,并打印到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. 请注意,“ getline”功能中使用了定界符:制表符(字符“ \\ t”)用于标记数据字段的结尾,而换行符“ \\ n”用于标记行尾。 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. “窥视”功能检查流中的下一个字符,如果没有更多的字符,它将设置流的“ eof”标志。 Because there could be more conditions that can invalidate stream for reading I use good() function as the condition in 'while' loop. 因为可能有更多条件可能会使读取流无效,所以我将good()函数用作“ while”循环中的条件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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