文件输入和过程数据

时间:2013-02-13 17:19:35

标签: c++ ifstream

我想从文本文件中读取文件格式

方法1
方法2
插入3“James Tan”

我目前正在使用ifstream打开文本文件并阅读项目,但是当我使用>>读取行,导致名称不能完全读作“James Tan”。 下面是代码和输出。

ifstream fileInput; 

  if(fileInput.is_open()){
       while(fileInput.good()){
  fileInput >>methondName>>value>>Name;
    ......

输出

methodName = Method, Method, Insert
value = 1, 2, 3 (must be a usigned integer)
Name = James

处理读取线条和内容的更好方法是什么。 有人告诉我getline。但我明白,getline完全是一行而不是单个单词。

接下来是fstream真快吗?因为,我想处理500000行数据,如果ifstream不快,我还有其他选择。

请就此提出建议。

1 个答案:

答案 0 :(得分:3)

  
    

方法1
    方法2
    插入3“James Tan”

  

我认为你的意思是该文件由几行组成。每行以“方法”或“插入”一词开头,每种情况后跟一个数字。此外,以“插入”开头的行最后会有一个多字的名称。

是吗?如果是这样,请尝试:

ifstream fileInput("input.txt");
std::string methodName;
int value;
while ( fileInput >> methodName >> value ) {
  std::string name;
  if(methodName == "Insert")
    std::getline(fileInput, name);

  // Now do whatever you meant to do with the record.
  records.push_back(RecordType(methodName, value, name); // for example
}