正在读取c++中的选项卡交付文件

Reading tab delimeted files in c++

本文关键字:选项 交付 文件 读取 c++      更新时间:2023-10-16

我正在做一个项目,确实需要一些帮助。我已经将变量存储到一个文件中,如下所示,但用制表符将它们分隔开。现在我想把它们读出来,作为单独的变量,所以问题是,如果没有标签,我该如何读取它们?我必须解析字符串吗?

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()<<"nr";
    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;
        }
     }
   }
}

非常感谢您的帮助。非常感谢。

而不是进行

while(!cfile.eof())

试试这个

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