ifstream不会打开文件 [英] ifstream won't open file

查看:179
本文介绍了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在同一目录中。我唯一能想到的是隐藏文件扩展名。为什么我无法打开文件?

解决方案

这是因为您打开blah.txt两次。 b
$ b

第一次:



ifstream input_file(blah.txt,ios :: in) code>



第二次:



input_file.open .txt)



删除第二个应该可以解决您的问题。


I'm trying to open a file so I can read from it.

#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;
    }
}

This is the code I have for reading the file named blah.txt and the output I'm getting at the terminal is the "Error". I am using Linux Mint 14 and gVim, so when I enter the :pwd command, I know I am in directory /mnt/share. Checking from the terminal, file blah.txt is in the same directory. The only thing I can think of is hidden file extensions. Why can't I open the file?

解决方案

That's because you open "blah.txt" twice.

First time:

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

Second time:

input_file.open("blah.txt")

Removing the second one should fix your problem.

这篇关于ifstream不会打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆