2013-10-16 3 views
1

텍스트 파일의 전자 메일을받은 순서대로 추가하는 링크 된 목록을 만들려고합니다. 이메일 정보를 검색하고 새 노드를 만드는 데 사용하는 while 루프는 단일 반복을 완료하지도 않습니다 (정수 및 중지 조건을 테스트 한 결과). 내 .txt 파일이 다른 사람의 프로그램과 함께로드 될 때 작동하므로 내 source_file.get이 작동하지 않는다고 생각합니까?파일로드 루프가 작동하지 않습니다.

int Classify::Load_email(char filename[]) { 
    ifstream source_file(filename); 
    char eof_prime; 
    if(!source_file) // Checks whether there is anything in the file 
     return 0; 
    if(!email_head) { // If there is no head, creates a new node for it to point to, and have tail point to it as well 
     email_head = new email_node; 
     email_tail = email_head; 
     email_tail->next = NULL; 
    } 
    source_file >> eof_prime; // Primes the eof function. 

    while(!(source_file.eof())); { 
     source_file.get(email_tail->email_data.sent, 50, '|'); 
     source_file.get(email_tail->email_data.from, 50, '|'); 
     source_file.get(email_tail->email_data.to, 50, '|'); 
     source_file.get(email_tail->email_data.subject, 100, '|'); 
     source_file.get(email_tail->email_data.contents, 200, '|'); 

     // If source_file isn't eof, then create a new node, connect the nodes, then have tail point to it, make next NULL 
     if(!(source_file.eof())) { 
      email_tail->next = new email_node; // retaining the order emails were sent in, so always adding to the end 
      email_tail = email_tail->next; 
      email_tail->next = NULL; 
     } 
    } // end while loop 
    return 1; 
}; 
+1

한 가지 질문 :'std :: getline' 및'std :: string' 객체 대신'std :: ifstream :: get'과 C-string이 필요한 이유는 무엇입니까? – LihO

+0

이 수업의 선생님은 우리가 아직 문자열을 사용하는 것을 허용하지 않습니다. 그녀는 지금 우리가 문자 배열을 사용한다고 주장합니다. getline에 문자 배열을 사용할 수 있습니까? –

+0

이 경우 당신의 접근 방식은 괜찮습니다 :) – LihO

답변

1

루프 그러므로이 보인다 때문에 스트림의 eof 플래그를 설정합니다 어떤 코드가 존재하지 않기 때문에 무한 루프가된다 ;

while(!(source_file.eof())); { 
         ^

을 잘못-놓고 빈 몸을 가지고 다음에 오는 코드는 절대로 실행되지 않습니다.

+0

std :: ifstream :: 스트림에서 제거 된 문자를 얻었습니다. –

+0

@DanMinor : 세미콜론 만 제거하십시오. – LihO