2014-11-27 7 views
1

텍스트 파일을 열려고 시도하고 있으며, 여기에서 각 행을 읽고 각 단어 어커런스가있는 행 번호에 매핑합니다. 그런 다음지도를 인쇄하고 싶습니다. 내 코드는 다음과 같습니다.C++ : fstream, 맵핑 및 출력 사용

#include <map> 
#include <set> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <sstream> 
using namespace std; 
int main(){ 

std::map<string, set<int>> myMap; 
ifstream myFile; 
string myLine =""; 
int lineNum = 0; 
stringstream myStream; 
string myWord =""; 
set<int> mySet; 

myFile.open("myTextFile.txt"); 
if(myFile.is_open()){ 
    while (!myFile.eof()){ 
     getline(myFile, myLine); 
     myStream.str(myLine); 
     ++lineNum; 
     while (!mySStream.eof()) 
     { 
      myStream >> myWord; 
      myMap[myWord].insert(lineNum); 

     }   
     myStream.clear(); 
    } 
} 
myFile.close(); 
// at this point, I expect everything to have been mapped 
// mapping each word occurrence to a line 
//I now want to print the map but out does not work, and I need to use an iterator 
//I have tried this: 
map<string, set<int>>::iterator iter; 
for (iter = myMap.begin(); iter != myMap.end(); ++iter) 
{ 
    cout << "Key: " << iter->first << endl << "Values:" << endl; 
    set<int>::iterator setIter; 
    for (setIter = iter->second.begin(); setIter != iter->second.end(); ++setIter) 
     cout << " " << *setIter<< endl; 
} 
return 0; 

} 

출력이 없습니다. 왜 안돼? 둘째, 모든 것이 올바르게 매핑되고 있습니까?

+2

'while (! myFile.eof()) Nope. – ildjarn

+0

[왜 루프 상태에서 iostream :: eof가 잘못된 것으로 간주 되는가?] (0120-13753) –

+1

[std :: endl'의 과도한 사용을 중지하십시오] (http : //kuhllib.com/2012/01/14/stop-excessive-use-of-stdendl/) –

답변

1

개발 기계가 아니기 때문에 테스트하지 못했습니다. 당신이하려는 일을 이해한다면 이것은 효과가있을 것입니다. 기본적으로 EOF 비트를 검사하는 대신 getline을 while 루프로 옮겼습니다. std :: getline은 전체 파일을 읽은 후 루프를 종료합니다!

#include <map> 
#include <set> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <sstream> 

using namespace std; 
int main(){ 
std::map<string, set<int>> myMap; 
ifstream myFile; 
string myLine =""; 
int lineNum = 0; 
stringstream myStream; 
string myWord =""; 
set<int> mySet; 

myFile.open("myTextFile.txt"); 

if(myFile.is_open()){ 
    while (getline(myFile, myLine)){ 
     myStream.str(myLine); 
     ++lineNum; 
     while (myStream >> myWord) 
     { 
      myMap[myWord].insert(lineNum);  
     }   
     myStream.clear(); 
    } 
    myFile.close(); 
} 
else 
{ 
    std::cout << "Unable to open file!" << std::endl; 
} 

map<string, set<int>>::iterator iter; 
for (iter = myMap.begin(); iter != myMap.end(); ++iter) 
{ 
    cout << "Key: " << iter->first << endl << "Values:" << endl; 
    set<int>::iterator setIter; 
    for (setIter = iter->second.begin(); setIter != iter->second.end(); ++setIter) 
    { 
     cout << " " << *setIter<< endl; 
    } 
} 
return 0; 

} 
+0

테스트 할 수 있습니다. 만약 당신이 입력 파일의 사본을 줄 수있는 프로그램. – yash101

+0

출력이 전혀 들지 않습니다. 또한 왜 while (! myFile.eof())를 사용할 수 없습니까? 나는 이런 식으로 가르쳤다. – JCoder

+0

출력이 없습니다. 아마도 파일이 열려 있지 않은 것입니까? [링크] (http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong?lq=1)에 따르면 eof()가 나중에 확인됩니다. 반면에이 코드는 좀 더 간결합니다. std :: getline() 및 연산자 >>() 모두 fail()을 확인합니다. – yash101