简体   繁体   中英

Why doesn't the program print to file?

i'm doing an homework and the text ask me to print some info on a file ".txt". I don't know why, after i wrote on the program the link of the file on which i want to copy the info, the ".txt" file is empty and doesn't show me anything. Eclipse tells me that the code is without error. This is the part of code of the print on file:

void stampaVendute(string& vendute,int& n,Opere f[],char p[],int a){
    cout<<"\nInserisci il file sul quale vuoi visualizzare le opere vendute: "<<endl;
        getline(cin,vendute);

        ofstream ofs;
        ofs.open(vendute.c_str());
        if(!ofs.good()){
                cout<<"C'è qualche problema nell'apertura del file"<<endl;
            return;
        }
        for(int i=0;i<n;i++){
            if((!stricmp(p,f[i].N_C)) and a<=f[i].anno){
            ofs<<"\nOPERA "<<(i+1)<<endl;
            ofs<<"Codice: "<<f[i].codice<<";"<<endl;
            ofs<<"Titolo: "<<f[i].titolo<<";"<<endl;
            ofs<<"Autore: "<<f[i].N_C<<";"<<endl;
            ofs<<"Anno: "<<f[i].anno<<";"<<endl;
            ofs<<"Valore: "<<f[i].prezzo<<";"<<endl;
        }
        ofs.close();
    }
    cout<<"\nI DATI SONO STATI COPIATI CORRETTAMENTE SUL FILE!"<<endl;
}

I don't know if this part of code is enought to solve my problem, but thank you anyway if you can help me :)

If if((!stricmp(p,f[i].N_C)) and a<=f[i].anno){ fails the test then nothing is printed to the file. Add a line that unconditionally prints to the file after opening, to see if it works.

Print the file name to the user when the file is opened successfully.

If you are using Windows, you can use process monitor from SysInternals (now owned by Microsoft) to see what file is actually being opened.


ofs.open(vendute.c_str());
why are you using c_str here? You know that open takes a std::string normally! You are getting a pointer to the raw characters just to construct a new different string object, and forcing the compiler to re-count the number of characters (calling strlen() again).

You should write it as one line anyway. Initialize the variable when you define it:

ofstream ofs {vendute};

⧺SL.io.50 Don't use endl .


       for(int i=0;i<n;i++){
        ... many uses of `f[i]` follows

You should be using a range-based for loop rather than subscripts, but the way you are passing the data (separate pointer to the first element and length) instead of simply passing a collection prevents that. This is not a good way to do things! See Standard Guidelines ⧺I.13 etc.

If you did need to go over the collection via subscripts, don't keep subscripting it over and over and over. Make a reference variable pointing to that spot:

for(int i=0;i<n;i++){
   const auto& x = f[i];
   ... now use x over and over, not f[i]

here's your problem

Look where ofs.close(); is being called. The indentation of the code in the post is all messed up... it looks like this should be after the loop, before the final cout line. But what's that extra } coming from?

You are closing the file after the first iteration through the loop. If that case ( i==0 ) did not print results, nothing will ever be shown.

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