2013-02-17 3 views
2

파일을 열어 읽을 수 있습니다.ifstream에서 파일을 열 수 없습니다.

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
using namespace std; 

ifstream input_file("blah.txt", ios::in); 
ofstream output_file("output.txt", ios::out); 

Bank::Bank(void){ 
    input_file.open("blah.txt"); 
    if(!input_file){ 
     cerr << "Error" << endl; 
     exit(1); 
    } 
    else{ 
     cout << "good 2 go" << endl; 
    } 
} 

이 파일은 blah.txt라는 파일을 읽는 데 필요한 코드이며 터미널에서 출력되는 내용은 "오류"입니다. 나는 Linux Mint 14와 gVim을 사용하고 있으므로, : pwd 명령을 입력하면, 나는/mnt/share 디렉토리에 있다는 것을 압니다. 터미널에서 확인하면 blah.txt 파일이 같은 디렉토리에 있습니다. 내가 생각할 수있는 유일한 방법은 숨겨진 파일 확장입니다. 파일을 열 수없는 이유는 무엇입니까?

답변

5

"blah.txt"를 두 번 열었 기 때문입니다.

처음 :

ifstream input_file("blah.txt", ios::in)

두 번째 시간 :

input_file.open("blah.txt")

문제를 해결해야 두 번째 제거.

0

ifstream input_file("blah.txt", ios::in); 

should open the file : 또한

번째 생성자 버전이 사용되는 경우, 스트림은 실제 파일과 연결되어있는 경우와 같은 오픈 멤버 함수를 호출 매개 변수가 만들어졌습니다.

input_file.open("blah.txt"); 

should fail : 오브젝트가 이미 연관된 파일 (오픈)가있는 경우

함수는 실패한다.

설명서를 읽으십시오.