我對C++和編程一般都比較陌生,所以我猜測我的錯誤是一個非常簡單的錯誤。無論如何,我一直致力於從.txt文件中掃描DNA序列,並且正在嘗試對其進行編程,以便用戶可以從命令行指定數據文件的名稱。我包括了整個函數供參考,但是我得到的特定問題是我一直無法讓文件實際打開,程序總是返回「Unable to open file」消息。我遇到問題的部分(我認爲)是最後一個循環,但是我包含了整個函數供參考。ifstream C++的問題
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;
}
}
}
請讓我知道是否有你需要的任何額外信息或任何我應該研究,這樣我可以更好地幫助自己!
謝謝!我更新了我的代碼,但它仍然沒有掃描這些文件,但最終肯定會造成問題。我添加了一些打印語句,矢量文件名顯示正確。我的數據文件與我的exe文件保存在相同的位置,所以它的位置正確,對嗎? – jblazer28
當前工作目錄很可能從父進程繼承,而不是設置到'.exe'的位置。嘗試使用絕對路徑作爲您的文件名(例如,如果您在Windows上,則使用'C:\ path \ to \ file') – Useless
。如果您通過在IDE中推送Play來使用Visual Studio調試器,默認情況下它會在項目目錄中啓動您的應用程序。請參見[項目屬性 - >配置屬性 - >調試 - >工作目錄]但是,如果您在Windows資源管理器中單擊* .exe文件,那麼工作目錄將是exe目錄。 – bitwise