2016-09-05 4 views
-2

두 줄의 텍스트 파일 사이에 동일한 줄을 찾습니다.getline은 같은 줄을 읽습니다 (C++, istream)

처음에는 잘 작동하지만 두 번째 것은 처음 부분 텍스트를 읽은 다음 종료합니다. 나는 다른 텍스트를 시도했지만 그다지 효과가 없습니다.

모든 코드를보고 싶은 경우

void similars(string text1,string text2){ 
    string str1,str2; 
    ifstream texta(text1.c_str());   
    ifstream textb(text2.c_str()); 

    if(texta.is_open() && textb.is_open()){ 
     while (getline (texta,str1)){ 
      while (getline (textb,str2){ 
       cout<<str1<<str2<<endl; 
      } 
     } 
    } 
    else cout << "Unable to open file"; 

} 
이 예를 고려 하지 말아야 할 일들을 함께 사용하지 마십시오
+0

샘플 입력, 생성 된 출력 및 생성 된 출력의 문제점을 게시하십시오. –

+0

두 번째 while 루프에')'가 없습니다. BTW – vu1p3n0x

+0

그래, 방금 전에 봤어 : D thanks –

답변

0

:

#include <iostream> 
#include <string> 
#include <fstream> 
using namespace std; 


void similars(string text1, string text2) 
{ 
    string str1, str2; 
    ifstream texta(text1.c_str(), ios::in);   
    ifstream textb(text2.c_str(), ios::in); 

    cout << "text1: " << endl << endl; 
    while(!texta.eof()) 
    { 
     getline (texta, str1); 
     cout << str1 << endl; 
    } 

    cout << endl << endl; 

    texta.close(); // closing safely the file 

    cout << "text2: " << endl << endl; 
    while(!textb.eof()) 
    { 
     getline (textb, str2, '\n'); 
     cout << str2 << endl; 
    } 

    textb.close(); 

    cout << endl; 

} 

int main() 
{ 
    system("color 1f"); 

    string sText1 = "data1.txt"; 
    string sText2 = "data2.txt"; 
    similars(sText1, sText2); 

    system("pause"); 
    return 0; 
} 

단지 메모장 또는 두 개의 텍스트 파일을 만듭니다 텍스트 편집기에서 "text1.txt", "text2.txt"로 이름을 바꾼 다음 텍스트를 입력하고 저장하고 닫습니다. 그런 다음 프로그램을 실행하십시오.