2014-10-18 101 views
0

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

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

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

+2

我会建议使用tokeniser,无论是自己写一个或谷歌的一个(例如:升压包括一个) – UnholySheep 2014-10-18 15:27:15

+0

不要使用'eof'。使用'eof'总是错误的。 – molbdnilo 2014-10-18 15:28:44

+1

使用boost项目中的库,boost tokenizer,http://boost.2283326.n4.nabble.com/tokenizing-a-string-td2564907.html – user2485710 2014-10-18 15:33:42

回答

1

,而不是做

while(!cfile.eof()) 

试试这个

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