我对 C++ 和一般编程比较陌生,所以我猜我的错误是一个非常简单的错误。无论如何,我一直在从 .txt 文件中扫描 DNA 序列,我正在尝试对其进行编程,以便用户可以从命令行指定数据文件的名称。我包含了整个函数以供参考,但是我遇到的特定问题是我无法让文件实际打开,程序总是返回“无法打开文件”消息。我遇到问题的部分(我认为)是最后一个 for 循环,但我包含了整个函数以供参考。
int dataimport (int argc, char* argv[]){
vector<string> mutationfiles (argc -1); //mutationfiles: holds output file names
vector<string> filenames (argc - 1); //filenames: holds input file names
if (argc > 1){
for ( int i = 1; i < argc; ++i){
string inputstring = argv[i]; //filename input by user
filenames[i-1] = inputstring; //store input filename in vector
stringstream out;
out << inputstring << "_mutdata.txt"; //append _mutdata.txt to input file names
mutationfiles[i-1] = (out.str()); //store as output file name
inputstring.clear(); //clear temp string
}
}
else{
cout << "Error: Enter file names to be scanned" << endl;
system("PAUSE");
return EXIT_FAILURE;
}
for(int repeat = 0; repeat < argc; ++repeat){
ifstream myfile; //open input file
myfile.open (filenames[repeat].c_str());
ofstream myfile2; //open output file
myfile2.open (mutationfiles[repeat].c_str());
string all_lines;
if (myfile.is_open()){
while ( myfile.good() ){ //scan data
getline (myfile,all_lines,'\0');
}
myfile.close(); //close infile
}
else{ //error message
cout << "Unable to open file\n";
system("PAUSE");
return EXIT_FAILURE;
}
}
}
如果您需要任何其他信息或我应该研究的任何内容,请告诉我,以便我更好地帮助自己!