程序在exit()函数之前不会打印行吗? [英] Program will not print line before exit() function?

查看:88
本文介绍了程序在exit()函数之前不会打印行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的学校小项目,我们必须解决一些错误,并在发生错误时退出该程序.基本上,在else语句中,当表达式的计算结果为false时,就好像它不会将错误写入文件.如果我将其输出到控制台(通过cout)而不是将其写入文件,则可以正常工作,但是当我尝试将其写入输出文件时,它将无法工作.基本上,这是我的问题.我的教授要求所有输出都到文件中,这就是为什么我不能使用cout的原因.那么,为什么将其输出到控制台而不是文件?

I have a little project for school I am writing in C++, and we have to account for some error and just exit the program in the event that it happens. Basically, in the else statement when the expression is evaluated as false, it's like it won't write to the file the error. If I output it to the console (via cout) instead of writing it to the file it works just fine, but when I try to write it to the output file, it does not work. Basically, that is my question. My professor is requiring that all output is to the file, that's why I can't use cout. So why will it output it to the console, and not to the file?

P.S.我正在将其他内容输出到文件中,并且工作正常,因此,为了便于记录,我认为将其范围缩小到else语句中的一小段代码.

P.S. I am outputting other stuff to the file and it's working fine, so for the record I think it is narrowed down to the little block of code in the else statement.

if(tempUnit == 'C' || tempUnit == 'F')
    {
      if(tempUnit == 'F')
      {
        temp = convertTemp(temp, tempUnit);
      }

      temps[a] = temp;

      printTemp(outputFile, tempUnit, temp);
      printTimestamp(outputFile, humanDate);

      outputFile << endl;

    }
    else{
      // this is where it doesnt work
      outputFile << "Error. Incorrect temperature unit, must be " << 
      "either a capital C or a capital F. Program ended.";
      exit(0);
    }

推荐答案

您需要在退出程序之前刷新缓冲区.

You need to flush the buffer before exiting the program.

outputFile << "Error. Incorrect temperature unit, must be either a capital C or a capital F. Program ended." << std::flush;

outputFile << "Error. Incorrect temperature unit, must be either a capital C or a capital F. Program ended." << std::endl;

这篇关于程序在exit()函数之前不会打印行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆