2012-08-29 4 views
0

내 코드의 마지막 부분에서 ofstream 변수에 문제가 있습니다. ofstream에서 정보를 잃지 않으려면 어떻게해야합니까? 문제는 프로그램의 마지막 부분에서 생성됩니다. 돈을 오류가 여기에있다, 그러나 I :ofstream이 모든 데이터를 잘리지 않습니다.

Action Movies 

Comedy Movies 

Drama Movies 

Horror Movies 
There are no movies for this genre 

Mystery Movies 
There are no movies for this genre 

Romantic Movies 

Adventure Movies 

Western Movies 

ife 1946 D 130 30 8.6 1946 103101 
The Best Years of Our Lives 1946 R 172 181 8.1 1946 17648 
Lawrence of Arabia 1962 V 216 48 8.5 1962 80746 
For a Few Dollars More 1965 W 132 123 8.2 1965 45515 

을 그리고 여기에 내가 점점해야합니다 것입니다 : 여기

Action Movies 
Braveheart 1995 A 177 88 8.3 1995 245089 
Batman begins 2005 A 140 110 8.3 2005 275523 

Comedy Movies 
Amelie 2001 C 122 45 8.5 2001 185124 
Singin' in the Rain 1952 C 103 77 8.3 1952 56368 

Drama Movies 
it's a Wonderful Life 1946 D 130 30 8.6 1946 103101 

Horror Movies 
There are no movies for this genre 

Mystery Movies 
There are no movies for this genre 

Romantic Movies 
The Best Years of Our Lives 1946 R 172 181 8.1 1946 17648 

Adventure Movies 
Lawrence of Arabia 1962 V 216 48 8.5 1962 80746 

Western Movies 
For a Few Dollars More 1965 W 132 123 8.2 1965 45515 

내 인쇄 기능입니다 여기

내가 얻고 무엇인가

'는 t는

void movieType::printMovieInfo(char* outFileName) 
{ 
std::ofstream outFile; 
if(!outFile.is_open()) 
    outFile.open(outFileName, std::ios::app); 

outFile<<name<<" "<<year<<" "<<genre<<" "<<length<<" "<<rank; 
outFile<<" "<<weight<<" "<<year<<" "<<votes<<std::endl; 

} 

가 편집 해결하는 방법을 알고

void movieType::printMovieInfo(std::ofstream& outFile) 
    { 
    outFile<<name<<" "<<year<<" "<<genre<<" "<<length<<" "<<rank; 
    outFile<<" "<<weight<<" "<<year<<" "<<votes<<std::endl; 
    } 

메인의 마지막 부분에서: 문제는 프로그램의 끝에서 생성되는

#include "movieType.h" 
#include <iostream> 
#include <fstream> 
#include <string> 
#include "PQType.h" 
#include "QueType.h"//FIFO queue 

using namespace std; 

int main() 
{ 
movieType movie[9]; 
ifstream inFile("movie1.txt"); 
ofstream outFile("printList.txt"); 

int i =0; 
bool notDone=true; 
while (notDone) 
{ 
    if (movie[i++].readMovieInfo(inFile)== false) 
     notDone=false;  
} 

PQType<movieType> PQqueue(8); 

int j=0; 
while(!PQqueue.IsFull()) 
{ 
    PQqueue.Enqueue(movie[j++]); 
} 

QueType<movieType> fifoQueue[8]; 

string Genre[8]={"Action", "Comedy", "Drama", "Horror", "Mystery", "Romantic", "Adventure","Western"}; 

movieType it; 
while (!PQqueue.IsEmpty()) 
{ 
    PQqueue.Dequeue(it); 

    if (it.getGenre() == 'A') 
     fifoQueue[0].Enqueue(it); 
    else if (it.getGenre() == 'C') 
     fifoQueue[1].Enqueue(it); 
    else if (it.getGenre() == 'D') 
     fifoQueue[2].Enqueue(it); 
    else if (it.getGenre() == 'H') 
     fifoQueue[3].Enqueue(it); 
    else if (it.getGenre() == 'M') 
     fifoQueue[4].Enqueue(it); 
    else if (it.getGenre() == 'R') 
     fifoQueue[5].Enqueue(it); 
    else if (it.getGenre() == 'V') 
     fifoQueue[6].Enqueue(it); 
    else if (it.getGenre() == 'W') 
     fifoQueue[7].Enqueue(it); 
} 

//Problem is generated here. 
movieType ij; 
for (int i=0;i<8;++i) 
{ 
    outFile<<Genre[i]<<" Movies"<<endl; 

    if (fifoQueue[i].IsEmpty()) 
     outFile<<"There are no movies for this genre"<<endl; 

    for(int j=0; fifoQueue[i].IsEmpty() != true; ++j) 
    { 
     fifoQueue[i].Dequeue(ij); 
     ij.printMovieInfo("printList.txt"); 
    } 

    outFile<<endl; 
} 
    return 0; 
} 
:

ij.printMovieInfo("printList.txt"); 

여기
ij.printMovieInfo(outFile); 

로 변경 내 주요 기능입니다

+1

왜 두 번 파일을여시겠습니까? ofstream을 printMovieInfo 함수로 전달하십시오. – Aleks

+1

'movie1.txt'의 각 데이터를 읽으면 출력물을 콘솔에 출력하려고 했습니까? 제대로 모으고 있는지 확인 하시겠습니까? – ChiefTwoPencils

답변

2

코드를 빠르게 읽은 후 필자가 파일을 작성하기 위해 파일을 여는 것으로 나타났습니다. 두 번. 처음에는 주 함수의 시작 부분에 있고 두 번째 부분은 printMovieInfo 함수에 있습니다.

2 개의 스트림이 서로 다른 위치에있을 수 있으므로 파일을 두 번 열어서는 안됩니다.

당신이 할 수있는 일은 주 기능에있는 귀하의 ofstream을 printMovieInfo 기능에 참조로 전달하는 것입니다. 잘하면 그 문제를 해결해야합니다.

코드를 한눈에 살펴보면 printMovieInfo를 호출하기 전에 스트림을 플러시 할 수 있습니다. 나는 여전히 첫 번째 옵션을 추천합니다.

그렇지 않으면 Roberto의 질문에 따라 입력 파일의 모든 데이터를 가져 오지 못할 수 있습니다.

+0

확인. 고마워. – user1561949

관련 문제