简体   繁体   English

为什么程序不打印到文件?

[英]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".我正在做作业,文本要求我在文件“.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.我不知道为什么,在我在程序上写下我想要复制信息的文件的链接后,“.txt”文件是空的,没有显示任何内容。 Eclipse tells me that the code is without error. Eclipse 告诉我代码没有错误。 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.如果if((!stricmp(p,f[i].N_C)) and a<=f[i].anno){测试失败,则不会向文件打印任何内容。 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.如果您使用的是 Windows,您可以使用 SysInternals(现在归 Microsoft 所有)的进程监视器来查看实际打开的文件。


ofs.open(vendute.c_str());
why are you using c_str here?你为什么在这里使用c_str You know that open takes a std::string normally!你知道open通常需要一个std::string 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).你得到一个指向原始字符的指针只是为了构造一个新的不同的字符串对象,并强制编译器重新计算字符数strlen()再次调用strlen() )。

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 . ⧺SL.io.50不要使用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.您应该使用基于范围的for循环而不是下标,但是您传递数据的方式(指向第一个元素和长度的单独指针)而不是简单地传递集合阻止了这种情况。 This is not a good way to do things!这不是做事的好方法! See Standard Guidelines ⧺I.13 etc.参见标准指南⧺I.13等。

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();看哪里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.帖子中代码的缩进全乱了……看起来应该是在循环之后,在最后的cout行之前。 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.如果这种情况 ( i==0 ) 没有打印结果,则不会显示任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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