用C++写一个简单的记录程序

#define WINVER 0x0A00       //run enviroment is win10

#include<fstream>
#include<iostream>
#include<afxwin.h>  //for messagebox
#include<time.h>    
#include<io.h>      //for access()

using namespace std;

char path[] = "D:/log.txt";

bool check()
{
    if(access(path, 0) == 0)
    {
        //cout<<path<<":  文件存在"<<endl;
        return true;
    }
    else
    {
        return false;
    }
}

void main(int argc, char* argv[])
{
    time_t rawtime;
	struct tm timeinf;
	
	time(&rawtime); 
	localtime_s(&timeinf,&rawtime);
    
	
    if(argc == 2)
    {
        cout<<"argv[1]: "<<argv[1]<<endl;
        
        if(!check())
        {
            fstream ff(path, ios::trunc | ios::out);
            ff.close();
        }
        fstream f1(path, ios::app);
        
        f1<<timeinf.tm_year +1900<< "-" << timeinf.tm_mon +1<< "-" << timeinf.tm_mday;
        f1<< " " <<timeinf.tm_hour << ":" << timeinf.tm_min << ":" << timeinf.tm_sec<<"\t=>  ";
        
        f1<<argv[1]<<endl;
        f1.close();
        
    }
    else
    {
        MessageBoxA(NULL, "一次只能输入一个数据", "", MB_OK); 
    }
}

编译后设置一下环境变量,然后就可以通过win+r快速运行这个程序了。

 

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐