在c ++中读取制表符分隔文件

时间:2014-10-18 15:23:59

标签: c++ file parsing delimiter string-parsing

我正在做一个项目,可以真正使用一些帮助。我已将变量存储到文件中,如下所示,但使用制表符将它们分开。现在我想把它们作为单个变量读出来,所以问题是,如何在没有标签的情况下阅读它们?我是否必须解析字符串?

void fileManagement::appendCustomeRFile(const Customer &obj)
{
  if(tryFile(CFile))
  {
    ifstream cfile(CFile);
    cfile<<obj.ReturnCustomerId()<<"\t"<<obj.ReturnFName()<<"\t"<<obj.ReturnLName()<<"\t"<<obj.ReturnContactNumber()<<"\t"<<obj.RetrunBalance()<<"\n\r";
    cfile.close;
  }
}

这就是我目前正在阅读的文件。

void viewCustomerInfo(string contact_N)//
{
 tryFile(CFile);
 bool found=false;
 string retreivedId,retreivedFname, retreivedLname, retreivedNumber;
 double retreivedBalance;
 ofstream cfile(CFile);
 if(cfile.is_open())
  {
    while(!cfile.eof())
    {
        cfile>>retreivedId>>retreivedFname>>retreivedLname>>retreivedNumber>>retreivedBalance;
        if (retreivedNumber==contact_N)
        {
            found=true;
            cout<<"_______________________________________CUSTOMER INFO_______________________________________\n"<<"Customer ID: "<<retreivedId<<"\nFirst Name: "<<retreivedFname<<"\nLast Name: "<<retreivedLname<<"\n";
            cout<<"Contact Number: "<<retreivedNumber<<"\nCustomer Balance"<<retreivedBalance<<endl;
            cout<<"___________________________________________________________________________________________"<<endl;
            break;
        }
     }
   }
}

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

而不是

while(!cfile.eof())

试试这个

 while(cfile>>retreivedId>>retreivedFname>>retreivedLname>>retreivedNumber>>retreivedBalance)