mardi 30 juin 2015

One ifstream variable, multiple files c++ (programming help)

Can you use one ifstream variable to have multiple files?

I have a program that successfully reads all the files in a directory. However, while reading, the program only reads the first file, and puts all its lines into a vector. All the other files are not read. Basically, I am trying to identify all the functions in a c++ file.

I tried to use the clear() function but it still doesn't work. Help please!

Here is the code:

void getDefinitions(bool start){
    if (start == true){

        //vector<string> filesInDirectory;//files in the directory

        string input;

        cout << "\n\nEnter The Folder Location For Arduino Cores:";
        cin >> input;
        string dir = string(input);
        vector<string> files;// = vector<string>();

        getdir(dir,files);

        if (showVerbose == true){
            cout << "\nFiles Found in The Specified Folder: \n";
            for (unsigned int i = 0;i < files.size();i++) {
                cout << files[i] << endl;
            }

        }

        for (int i = 0; i < files.size();i++){
            if (files[i].find(".h") != string::npos){
                files.erase(files.begin() + i);
            }
        }

        for (int i = 0; i < files.size();i++){
            if (files[i].find(".h") != string::npos){
                files.erase(files.begin() + i);
            }
        }
    //do this multple times because doing it once misses some files for some reason

        for (int i = 0; i < files.size();i++){
            if (files[i].find(".h") != string::npos){
                files.erase(files.begin() + i);
            }
        }

    //erasing .h files as they are not needed

        if (showVerbose == true){
            cout << "\nFiltered Files: \n";
            for (unsigned int i = 0;i < files.size();i++) {
                cout << files[i] << endl;
            }

        }

        int iteratora = -1;

        string line;//variable for holding the characters in one line
        vector<string> programFile = vector<string> ();
        vector<string> librariesIncluded;



        for (unsigned int i = 0; i < files.size();i++ ){//for loop start

    iteratora++;
    string filename1 = files[iteratora];

        cout << "\n" << filename1 << ".\n";
        cout << "Iterator: " << iteratora << ".\n";

    programFile.clear();
    fstream afile (filename1);//(filename1);


    string line;//variable for holding the characters in one line



    if (afile.is_open())
    {
        while ( getline (afile,line) )
        {
            //cout << line << '\n';
            if (line != ""){
            programFile.push_back(line);
            line = "";
            }

        }
        afile.close();
    }

    afile.clear();

   print_vector(programFile);//used to know what is in the file

   int iteratore = 0;

            for (int i = 0; i < programFile.size(); i ++){  
                string programLine = programFile[iteratore];
                iteratore++;


                vector<int> qPositions;
                vector<int> tPositions;

                if (programLine.find("#include") != string::npos){

                    int qpos = programLine.find('"', 0);

                    while (qpos != string::npos){
                        qPositions.push_back(qpos);
                        qpos = programLine.find('"',qpos + 1);
                    }

                    if (qPositions.size() != 0){
                        programLine = programLine.substr(qPositions[0],qPositions[1]);

                    //  librariesIncluded.push_back(programLine);
                    }

                    int tpos = programLine.find('<', 0);

                    while (tpos != string::npos){
                        tPositions.push_back(tpos);
                        tpos = programLine.find('>',tpos + 1);
                    }

                    if (tPositions.size() != 0){
                        programLine = programLine.substr(tPositions[0],tPositions[1]);

                        librariesIncluded.push_back(programLine);
                        finalLibraries.push_back(programLine);
                    }


                    if (showVerbose = true){
                        cout <<"\nFound libary '"<< programLine << "'.\n";
                    }




                }


            }

}//loop end





    system("pause");









    }


 }


int getdir (string dir, vector<string> &files)
{
    DIR *dp;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
        system("pause");
        exit(EXIT_FAILURE);
    }

    while ((dirp = readdir(dp)) != NULL) {
        files.push_back(string(dirp->d_name));
    }
    closedir(dp);
    return 0;
}

Thanks!

Aucun commentaire:

Enregistrer un commentaire