Question

I have two problem, the second file n_cartelle_mod.txt isn't completelly changed from n_cartelle.txt and the second problem is assignment nomi_txt[cont] = n_cartelle[i] why? the problem is in the for where is the substr and when found a txt file and after there is the assignment.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char** argv) {
    string arg = argv[2];                       
    int arch;                                   
    string cambia ="cd ";
    string cartella = argv[1];                  

    vector <string> n_cartelle;
    vector <string> nomi_txt;
    int cont = 0;


    //start code 

    if(argc == 3) {

        cambia += cartella.substr();
        cambia += " && dir > n_cartelle.txt";

        const char * c_cambia = cambia.c_str(); 


        arch = system(c_cambia);
        if(arch == 0)
            cout << "\n Cartella cambiata con successo"<<endl
                 <<"\n  Documento di testo con nome e tipo di file creato con successo"<<endl;
        else{

            cout << "\nErrore nella chiamata a SYSTEM"<<endl;
            exit(EXIT_FAILURE);
        }

        //decifro il file creato:

        //prendo le stringhe dal file-----------------------------------------------


        ifstream in("n_cartelle.txt");
        ofstream out("n_cartelle_mod.txt");
        string linea;

        while(getline(in,linea)){

            if(linea.size() < 2)
                out << "0000000000000000000000000000000000000000000000000\n";
            else
                out << linea<<"\n";

            //n_cartelle.push_back(linea);
        }
        cout << "File creato e modificato, vado avanti\n";

        string linea2;
        ifstream in2("n_cartelle_mod.txt");
        while(getline(in2,linea2))
            n_cartelle.push_back(linea2);

        cout << "Caricato il <vector>\n";

        //-------------------------END-------------------------------------------


        //problem is here

        for(int i=0; i < n_cartelle.size()-4; ++i){

            cout <<"i volte: "<<i<<endl;

            if(n_cartelle[i].substr(n_cartelle[i].size()-3) == "txt"){
                cout << "capperi\n";

                nomi_txt[cont] = n_cartelle[i];


                cont++;
            }


        }



    }


    else {
        cout << "Parametri inesatti.\n\n"       
             << "Esempio di utilizzo:\n        Search.exe c:\\\\User paolo"<<endl;

    }
Était-ce utile?

La solution 2

nomi_txt[cont] = n_cartelle[i];

Since you never set the size of nomi_txt, this will result in undefined behavior as you will be accessing elements out of the bounds of the vector. If you want nomi_txt to have the same number of elements as n_cartelle, you need to resize it:

nomi_txt.resize(n_cartelle.size());

prior to the loop.

Barmar already mentioned the issue with closing the std::ofstream prior to reopening the file for input.

Autres conseils

You need to close out before you do ifstream in2("n_cartelle_mod.txt");. Output to files is buffered by default, and the last block of output won't be flushed to the file until you do this.

Or you could use endl instead of "\n" in your out << lines; this outputs a newline and also flushes the buffer.

One problem that appears is that the last 2 vector items in n_cartelle are skipped by the for loop:

for(int i=0; i < n_cartelle.size()-4; ++i) {

Further, I believe the nomi_text should use a .push_back or similar function to add elements to it rather than an indexed assignment to indices that might not already exist.

You have to substitute this statement

nomi_txt[cont] = n_cartelle[i];

for

nomi_txt.push_back( n_cartelle[i] );

because vector nomi_txt is empty and you may not use the subscript operator to access elements that do not exist.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top