0

私は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;
        }
    }
}

あなたが必要とする追加情報や私が自分自身をより良く助けることができるように私が研究すべき何かがあるかどうか私に知らせてください!

4

2 に答える 2

1
for(int repeat = 0; repeat < argc; ++repeat)

する必要があります

for(int repeat = 0; repeat < argc - 1; ++repeat)

それを除けば、あなたが受けているエラーの原因となるものは何も見えません。

それを修正してもエラーが発生する場合は、名前を印刷して、2つのベクトルの内容が正しいことを確認してみます。

for(int repeat = 0; repeat < argc - 1; ++repeat)
{
    cout << filenames[repeat] << endl;
    cout << mutationfiles[repeat] << endl;
}
于 2012-09-05T17:50:09.833 に答える
-1

for ( int i = 1; i < argc; ++i) に変更for ( int i = 0; i < argc; i++)

filenames[i-1] = inputstring;に変更filenames[i] = inputstring;

に変更mutationfiles[i-1]mutationfiles[i]ます。

于 2012-09-05T17:50:46.600 に答える