2017-12-30 7 views
-2

電話帳をプログラムしていますが、ファイルの読み込みに問題があります。 エラーがある:[保留]「演算子>>」の一致(オペランドの型が「のstd :: basic_istream」と「」です)'operator >>'(オペランドの種類は 'std :: basic_istream'と '')と一致しません

は、この関数は、txtファイルに書き込みのためではありません:

void PhoneBook::Save(){ while(1) 
{ 
ofstream file; 
file.open("test.txt",ios::app); 
     file<<contact.first<<endl 
       <<contact.last<<endl 
       <<contact.areacode<<endl 
       <<contact.number<<endl 
       <<contact.email<<endl 
       <<contact.webaddr<<endl 
       <<contact.address; 

    }} 

と負荷の関数である:

void PhoneBook::Load() 
{ 
    ifstream file("test.txt"); 
           //how i can make it(reading from file)correct? 
    if (file.is_open()) 
    { 
     file>>contact.first>>endl  
      >>contact.last>>endl  
        >>contact.areacode>>endl 
        >>contact.number>>endl 
        >>contact.email>>endl 
        >>contact.webaddr>>endl 
        >>contact.address; 
    } 
    else 
     cout<<"error in openening file"; 

} 
+3

http://idownvotedbecau.se/itsnotworking/ – user0042

答えて

3

std::endlを出力するためではなく、入力のために使用されます。

operator>>は、入力に表示される他の空白とともに、とにかく改行をスキップします。

0

ボーが述べたように、std::endlは入力用ではありません。本当に使いたいのはstd::getline(std::istream, std::string &)です。

std::getline(file, contact.first); 
std::getline(file, contact.last); 
std::getline(file, contact.the_rest_of_the_strings_in_contact); 

これは、改行文字(あなたが望むように見える)まで読み込みます。

関連する問題