2014-02-27 2 views
0

단일 txt 파일을 읽는 것으로 여러 개의 이진 트리를 만들려고합니다. 이렇게하려면 줄의 각 문자를 읽고 트리를 만든 다음 다른 트리를 만들기 위해 다음 줄로 이동해야합니다. 현재 코드가 입력 줄을 한 줄씩 잡아 내고 있으며이를 변경하는 방법을 모르겠습니다. , std::noskipws 초기 공백 문자를 추출 할 수있는 유효한 내용을 의미문자와 문자를 한 줄씩 문자별로 txt 파일을 읽는 방법 C++

int main() 
{ 
    vector <BinaryTree <string> > BT; 
     int iteration = 0; 

     string line; 
     ifstream myfile("input.txt"); 
     if (myfile.is_open()) 
     { 
      while(getline (myfile, line)) 
      { 
       BinaryTree <string> temptree; 
       BT.push_back(temptree); 
       BT[iteration].InsertData(line); 

       cout << "Preorder: "; 
       BT[iteration].PrintPreorder(); 
       cout << endl; 
       cout << "Inorder: "; 
       BT[iteration].PrintInorder(); 
       cout << endl; 
       cout << "Postorder: "; 
       BT[iteration].PrintPostorder(); 
       cout << endl; 
       cout << "Reverse Inorder: "; 
       BT[iteration].PrintReverseInorder(); 
       cout << endl; 

       BT[iteration].PrintPrintTree(); 
       cout << endl; 
       iteration++; 


      myfile.close(); 
     } 
     return 0; 
    } 

답변

0
char c; 
while (myfile>> std::noskipws >> c) 
{ 
    if(c == '\n') 
     printf("new line\n"); 
    printf("%c\n", c); 
} 

하면 줄 바꿈을 얻는 경우에 문자로 문자를 읽고 다음 테스트 할 operator>>를 사용

내가 지금 가지고있는 것입니다 . 또는 다음과 같이 ifstream.get()을 사용할 수도 있습니다 :

char c; 
while (myfile.get(c)) 
{ 
    if(c == '\n') 
     printf("new line\n"); 
    printf("%c\n", c); 
} 
+0

또한'char c = ifs.get()'을 사용할 수도 있습니다. –

관련 문제