简体   繁体   中英

Problems with ifstream C++

I'm relatively new to C++ and programming in general, so I'm guessing that my mistake is a pretty simple one. Anyways, I've been working on scanning DNA sequences in from .txt files and I am trying to program it such that the user can specify the names of the data files from the command line. I included the entire function for reference, however the particular problem I'm getting is that I've been unable to get the file to actually open, the program always returns the "Unable to open file" message. The part with which I am having problems (I think) is the last for loop, but I included the entire function for reference.

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

Please let me know if there is any additional information you need or anything I should research so that I can better help myself!

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

should be

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

Aside from that I can't see anything that would cause the error you are getting.

If you fix that and still get errors, I would try printing the names to make sure the contents of your two vectors is correct.

for(int repeat = 0; repeat < argc - 1; ++repeat)
{
    cout << filenames[repeat] << endl;
    cout << mutationfiles[repeat] << endl;
}

Change for ( int i = 1; i < argc; ++i) to for ( int i = 0; i < argc; i++)

and

change filenames[i-1] = inputstring; to filenames[i] = inputstring;

and

change mutationfiles[i-1] to mutationfiles[i] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM