用户输入的文件从c ++中读取

时间:2018-02-12 23:40:20

标签: c++ string file input

我正在尝试使用变量dnaInput上的ifstream类型从文件中读取。我希望程序能够获取用户输入的内容,然后从他们所说的文件中读取。输入的类型为字符串

cout<<"Would you like to continue? (Y/N)"<<endl;
cin>>input;
while(input=='y'||input=='Y')
{
    cout<<"input the name of your file you would like to append to the current bryce.out.txt file."<<endl;
    cout<<"MAKE SURE YOU INCLUDE THE .TXT"<<endl;
    cin>>DNASource;
    dnaInput.open(DNASource.c_str());
    while (dnaInput.fail())
{
    cerr<<"Error opening file!"<<endl;
    cout<<"input the name of your file you would like to append to the current bryce.out.txt file."<<endl;
    cout<<"MAKE SURE YOU INCLUDE THE .TXT (NO SPACES)"<<endl;
    cin>>DNASource;
    dnaInput.open(DNASource.c_str());
}
}

1 个答案:

答案 0 :(得分:0)

再添加一个字符串变量并修改您的程序,如下所示

 std::string path="/yourpathtothefile/";

 cin>>DNASource;
 path = path + DNASource;
 dnaInput.open(path.c_str());