用户输入的文件将从C 读取

User inputed file to read from c++

本文关键字:读取 文件 输入 用户      更新时间:2023-10-16

我正在尝试使用变量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());
}
}

添加更多字符串变量,然后修改您的程序如下

 std::string path="/yourpathtothefile/";
 cin>>DNASource;
 path = path + DNASource;
 dnaInput.open(path.c_str());