小编use*_*249的帖子

C++ ifstream指针打开文件失败

它似乎不像ifstream*->open我预期的那样工作......这是代码:( g++ 4.7使用-std=c++11in 编译MAC OSX 10.7)

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
    string line;
    vector<string> fname = {"a.txt","b.txt"};
    vector<ifstream*> files ( 2, new ifstream );

    files[0]->open( fname[0] );
    getline( *files[0], line,  '\n');
    cerr<<"a.txt: "<<line<<endl; 
    //this one prints the first line of a.txt

    line.clear();

    files[1]->open( fname[1] );
    getline( *files[1], line, '\n'); 

    cerr<<"b.txt: "<<line<<endl;
    //but this one fails to print any from b.txt
    //actually, …
Run Code Online (Sandbox Code Playgroud)

c++ ifstream

2
推荐指数
1
解决办法
1666
查看次数

标签 统计

c++ ×1

ifstream ×1