2012-09-24 3 views
0

파일이 있는데 그 내용을 인쇄하고 싶지만 기능이 작동하지 않습니다. 아무도 도와 줄 수 있습니까?텍스트 파일의 내용을 인쇄하는 방법

int main() 
{ 
    MenuText text; 
    string test = "Champion"; 
    ofstream output("File.txt"); 
    text.save(output); 
    fstream output ("File.txt"); 
    text.load("File.txt");//This is an error. 
    text.print(); 


MenuText::MenuText() 
{ 
    mText = "Default"; 

} 
MenuText :: MenuText(string text) 
{ 
mText = text; 
} 
void MenuText::print() 
{ 
cout<< "Story= " << mText<< endl; 
cout<< endl; 
} 
void MenuText::save(ofstream& outFile) 
{ 
outFile<< "/   . \ \____ \\ \\ _ -. " 
      //"/ /__  - \/\_______\\__\\__\ \" 
      "__\ /\ __ \  .  \/_______//__//" 
      "__/\/__/ \ \ \_\ \  - ________ " 
      "___ ___ ______ \ \_____/  - /\ " 
      "__ \\ -.\ \\ ___\ \/_____/ ." 
      "\ \ \__\ \\ \-.  \\ __\_  " 
      "- \ _\_______\\__\ `\___\\_____\   " 
      ".  \/_______//__/ /___//_____/ "<< mText<< endl; 
cout<< endl; 
outFile<< endl; 
} 
void MenuText::load(ifstream& inFile) 
{ 
string garbage; 
inFile>> garbage >> mText; 
} 
The errors are: 
Error 1 error C2371: 'output' : redefinition; different basic types c:\users\conor\documents\college\c++ programming\marooned\marooned\mainapp.cpp 15 1 Marooned 
Error 2 error C2664: 'MenuText::load' : cannot convert parameter 1 from 'const char [9]' to 'std::ifstream &' c:\users\conor\documents\college\c++ programming\marooned\marooned\mainapp.cpp 16 1 Marooned 
+0

파일을 다시로드 할 때 오류를 알려주십시오. –

답변

2

MenuText::load()가 유일한 인수가 아닌 const char* 같은 ifstream&을 취

여기 내 코드입니다. ifstream의 인스턴스를 생성하고 MenuText::load()에 전달 : 또한

std::ifstream input("File.txt"); 
if (input.is_open()) 
{ 
    text.load(input); 
} 

, close()output 스트림을 ifstream를 작성하기 전에 플러시 기록 된 모든 데이터를 확인 할 수 있습니다.

MenuText::load()은 파일의 전체 내용을 메모리로 읽지 않습니다. 파일에있는 두 번째 문자열을 mText에 저장합니다. operator>>은 첫 번째 공백 문자에서 읽지 않습니다.

+0

그게 효과가 :). 인쇄 방법 때문에 내용을 인쇄하지 않기 때문에 약간의 작업이 필요합니다. – Pendo826

+0

인쇄 방법을 수정하는 방법을 알려주세요. – Pendo826

+0

@ Pendo826,'print()'메소드가 올바르지 않습니다. 이것은'load()'메소드가 아닙니다. – hmjd

관련 문제