1. 程式人生 > >c++相關函數實現

c++相關函數實現

lena direct pro ret sub filename () time pfile

日誌打印

extern std::string GetProgramDir();
extern std::string GetProgramName();
extern int write_log(const char *format, ...);
int write_log(const char *format, ...)
{
    std::string log_name = GetProgramDir() + ‘\\‘ + GetProgramName() + ".log";
    FILE* pFile = fopen(log_name.c_str(), "a");
    va_list arg;
    int done;

    va_start(arg, format);

    time_t time_log = time(NULL);//#include <time.h>
    struct tm* tm_log = localtime(&time_log);
    fprintf(pFile, "%04d-%02d-%02d %02d:%02d:%02d ", tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday, tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec);

    done = vfprintf(pFile, format, arg);
    va_end(arg);
    fflush(pFile);
    fclose(pFile);
    return done;
}
std::string GetProgramDir()
{
    std::string program_file_name;
    char the_filename[MAX_PATH];  //#include <Windows.h>
    DWORD length = ::GetModuleFileName(nullptr, the_filename, MAX_PATH);
    if (length > 0 && length < MAX_PATH)
    {
        program_file_name = the_filename;
    }
    int pos = program_file_name.find_last_of(‘\\‘, program_file_name.length());
    return program_file_name.substr(0, pos);  // Return the directory without the file name
}
std::string GetProgramName()
{
    std::string program_file_name;
    char the_filename[MAX_PATH];
    DWORD length = ::GetModuleFileName(nullptr, the_filename, MAX_PATH);
    if (length > 0 && length < MAX_PATH)
    {
        program_file_name = the_filename;
    }
    int pos1 = program_file_name.find_last_of(‘\\‘, program_file_name.length());
    int pos2 = program_file_name.find_last_of(‘.‘, program_file_name.length());
    int len = pos2 - pos1 - 1;
    return program_file_name.substr(pos1 + 1, len);  // Return the directory without the file name
}

c++相關函數實現