美文网首页
__FILE__,__LINE__,__FUNCTION__ 的

__FILE__,__LINE__,__FUNCTION__ 的

作者: 风轻云淡宇 | 来源:发表于2021-08-24 09:42 被阅读0次

在C/C++语言中
__FILE__:打印相应的文件名
__LINE__:打印语句在源代码中相应的行
__FUNCTION__:打印语句在源代码中相应的函数名

下面代码可以打印出当前的文件名、代码行和函数名

#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

string GetPosition(string file, int line, string function)
{
    stringstream stream;
    stream << line;
    string str;
    int pos = file.rfind("\\");
    str = file.substr(pos + 1) + " " + stream.str() + " " + function;
    return str;
}

int _tmain(int argc, _TCHAR* argv[])
{
    cout << GetPosition(__FILE__, __LINE__, __FUNCTION__) << endl;
    return 0;
}

执行后效果如下:


执行后效果

相关文章

网友评论

      本文标题:__FILE__,__LINE__,__FUNCTION__ 的

      本文链接:https://www.haomeiwen.com/subject/shjriltx.html