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

标签 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;
        }
     }
   }
}

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

最佳答案

而不是做

while(!cfile.eof())

试试这个

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

关于c++ - 在 C++ 中读取制表符分隔的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26441340/

相关文章:

c - fork 再次写入文件句柄

c++ - 如何使用 boost 库更改文件所有权?

xml - 如何解析带有属性的xml文件?

c++ - 错误: expression cannot be used as a function?

c++ - 指针指向指针的实际用途?

python - 我在一个简单的 Python open() 函数上收到一个 File not Found 错误

regex - 当值也可以包含空格时,拆分包含以空格分隔的 "key=value"对的字符串

c++ - 为什么我应该在 C++ 中使用 typedef 声明一个枚举?

C++:set 如何知道两个项目何时相等?

python正则表达式将原始UDP数据读入3d点