2017-12-30 1 views
-2

전화 번호부를 프로그래밍하고 있는데 파일에서 읽는 데 문제가 있습니다. 오류는 없음에 대한 매치 '연산자 >>'이 함수는 TXT 파일에 쓰기위한'연산자 >>'에 일치하는 항목이 없습니다 (피연산자 유형이 'std :: basic_istream'및 '')

[대기 (피연산자 유형 '표준 : basic_istream보다는'및 '이다)

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

이렇게하면 줄 바꿈 문자 (원하는 모양)가 표시됩니다.

관련 문제