关于C++中ifstream in()以及ios_base::in的问题

代码如下:

void Utilities::readFile(const string file_name, vector<double>& histogram){
    ifstream in(file_name.c_str(), ios_base::in);
    if(in == NULL){
        cout << "Error:open data file error.\n";
        getchar();
        exit(0);        
    }
    string str;
    const string delimiters = " ";
    getline(in, str);
    tokenize(str, histogram, delimiters);
} 

运行结果如下:
运行结果

1.程序出错在打开文件上
2.之所以程序没有退出是调用了getchar()函数,在等待用户在终端输入字符
所以,建议尝试的程序改动是:
1.ifstream构造函数应该接收的是const char * 的类型,并非const string 类型,即:
readFile(const char *file_name, vector& histogram)
2.尝试使用二进制方式打开文件,即:
ifstream in(file_name.c_str(), ios_base::in|ios_base::binary);
题主可以尝试一下,如果可以将问题解决了,记得采纳哦。

1.为什么运行到输出“open data file error”之后就不再运行了,getchar();和exit()本应该走完退出才对。
2.既然会输出“open data file error”说明if条件语句成立,即"in==NULL"为真,但是实际上一步ifstresam in中in应该是已经读入数据了,为什么会为NULL呢?