[linux编程]常用小模块

news/2024/7/1 18:15:00

0:得到主机名和程序进程ID,系统当前时间

#include <QDebug>
#include <unistd.h>
#include <sstream>
#include <QString>
int main(int argc, char *argv[])
{
   // QApplication a(argc, argv);
   //  MainWindow w;
   //  w.show();

   // return a.exec();
    char hostname[80];
    int getHostName=gethostname(hostname,80);
    if (getHostName==0)
        qDebug()<<hostname;  //ubuntu-ht

    pid_t programPID=getpid();
    qDebug()<<programPID;

    std::ostringstream Ts;

    // varibale for time
    time_t seconds;

    // get current time
    seconds = time (NULL);
    struct tm * timeinfo;
    char logtime [80];

    timeinfo = localtime ( &seconds );
    // Format time string
    strftime (logtime,80,"%Y-%m-%d %X ",timeinfo);

    Ts << logtime;
    qDebug()<<QString(logtime);
    qDebug()<<QString(Ts.str().c_str());
    return 1;
}

2:以"filename"为文件名打开该文件,并写入内容

该程序的灵活之处在于logfn可根据不同情况变化文件名(不是最好的方法 )

     std::stringstream logfn;
     logfn << "filename";
     std::string log_file = logfn.str();    // stringstream 类型转化为 string类型
     const char* log_filec = log_file.c_str();   //string类型到char *类型

     std::ofstream zlog;
     zlog.open(log_filec);                
     zlog<<zTs()<<”logfile opened”<<std::endl;   

http://www.niftyadmin.cn/n/648280.html

相关文章

java replace t_Java replace() 方法

Java replace() 方法replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符&#xff0c;并返回替换后的新字符串。语法public String replace(char oldChar,char newChar)参数oldChar -- 原字符。newChar -- 新字符。返回值替换后生成的新字符串。实例public …

Python安装第三方库的方法以及Pycharm无法import安装好的第三方库的原因和解决方法

Python2.0系列已经在2020年01月01日停止支持。本文章主要介绍Python3.7安装第三方库的方法。 方法一&#xff1a;pip命令行直接安装 打开cmd命令窗口&#xff08;快捷键winR&#xff0c;然后输入“cmd”&#xff09;&#xff0c;通过命令 pip install packagename 进行第三库…

java加sql系统_学生成绩管理系统sql+Java

【实例简介】学生成绩管理系统sqlJava&#xff0c;数据库 用eclipse编写的&#xff0c;不要忘记引入jar包。【实例截图】【核心代码】ea336489-4b7e-4d27-a96d-6cea1daad467└── DatabaseExperiment-master└── DataBaseExperiment├── bin│ ├── Graphical_User_In…

opencv拼接相关1

这里面都是一些比较杂的东西&#xff0c;没什么实际意义。主要是为了&#xff0c;后面能跑一个程序&#xff1a; Stitcher&#xff1a; 抠细节&#xff1a; http://docs.opencv.org/2.4.2/modules/stitching/doc/high_level.html?highlightstitcher#stitcher Stitcher是啥&…

php fpm支持redis配置文件_如何让PHP支持Redis

原理&#xff1a;php默认扩展库不含有redis扩展&#xff1b;要支持redis扩展&#xff0c;需要有redis.so这个扩展文件所以我们的目标就是生成redis.so扩展文件&#xff0c;并修改php.ini 让其支持redis扩展。准备测试环境与软件&#xff1a;Vm虚拟机&#xff1b;CentOS5.5(已搭…

[android] 服务的生命周期(混合方式)

绑定服务&#xff1a;可以调用服务里面的方法&#xff0c; 如果调用者activity销毁了&#xff0c;服务也会跟着销毁 单独解除绑定的时候&#xff0c;服务也会被销毁 开启服务&#xff1a;不可以调用服务里面的方法 如果调用者activity退出了&#xff0c;服务还会存在 需求&…

Install SysBench support MySQL and PostgreSQL

[测试环境]CentOS 5.7 x64[安装MySQL]1. 下载MysqlRed Hat & Oracle Linux 5 (x86, 64-bit), RPM Package MySQL Server 5.6.11 84.2M Download (MySQL-server-5.6.11-2.rhel5.x86_64.rpm) MD5: 944e3e425becf3ef7ad5f191e0e1f04f 2. 安装Mysqlrpm -ivh MySQL-server-5.6…

lintcode:数字组合III

数字组合III 组给出两个整数n和k&#xff0c;返回从1......n中选出的k个数的组合。 您在真实的面试中是否遇到过这个题&#xff1f; Yes样例 例如 n 4 且 k 2 返回的解为&#xff1a; [[2,4],[3,4],[2,3],[1,2],[1,3],[1,4]] 解题 数字组合I 数组组合II 同样式DFS 本题只需…