C++ 程序写文件日志的简单方法

一、简介:

在C++ 程序调试过程中,有时为了把过程中的信息和数据保存下来,可以使用写文件日志的方法。

  • 头文件: #include <iostream> 

                       #include <fstream>

  • 声明: std::ofstream LogFile;
  • 打开文件:LogFile.open(“log_file.txt”, std::ofstream::out);  

1、C++ STL 中的定义:

void open (const   char* filename,  ios_base::openmode mode = ios_base::out);
void open (const string& filename,  ios_base::openmode mode = ios_base::out);

2、Qt 中的定义:

	void open(const char *_Filename,
		ios_base::openmode _Mode = ios_base::out,
		int _Prot = (int)ios_base::_Openprot)
		{	// open a C stream with specified mode
		if (_Filebuffer.open(_Filename, _Mode | ios_base::out, _Prot) == nullptr)
			_Myios::setstate(ios_base::failbit);
		else
			_Myios::clear();	// added with C++11
		}

	void open(const string& _Str,
		ios_base::openmode _Mode = ios_base::out,
		int _Prot = (int)ios_base::_Openprot)
		{	// open a C stream with specified mode
		open(_Str.c_str(), _Mode, _Prot);
		}

3、C++ STL 中的文档说明:

public member function

<fstream>

std::ofstream::open

filename

String with the name of the file to open.
Specifics about its format and validity depend on the library implementation and running environment.

mode

Flags describing the requested input/output mode for the file.
This is an object of the bitmask member type openmode that consists of a combination of the following member constants:

member constantstands foraccess
ininputFile open for reading: the internal stream buffer supports input operations.
out *outputFile open for writing: the internal stream buffer supports output operations.
binarybinaryOperations are performed in binary mode rather than text.
ateat endThe output position starts at the end of the file.
appappendAll output operations happen at the end of the file, appending to its existing contents.
trunctruncateAny contents that existed in the file before it is open are discarded.

These flags can be combined with the bitwise OR operator (|).
* out is always set for ofstream objects (even if explicitly not set in argument mode).
Note that even though ofstream is an output stream, its internal filebuf object may be set to also support input operations.

C++11: If the mode has both trunc and app set, the opening operation fails.

Return Value

none

If the function fails to open a file, the failbit state flag is set for the stream (which may throw ios_base::failure if that state flag was registered using member exceptions).

二、示例代码:

#include <iostream>
#include <fstream>
...

    std::ofstream LogFile;

    LogFile.open(“log_file.txt”, std::ofstream::out);

    ...

    LogFile << "-------------------- Algorithm 1: CalculateMaxAggrSum() --------------------" << "\n";
    LogFile << "Datetime: " << std::string(ctemp) << "\n";
    LogFile << "ImageFileName: " << this->ImageFileName << "\n";
    LogFile << "A1 score_rate: " << score_rate*100 << "%\n";
    LogFile << "---------------------------------------------------------------------" << "\n";

    ...

    LogFile.close();

 

 
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值