参考链接:C++中对文本文件的读写(iostream方法)
头文件:
#inclass="superseo">clude
#include //count函数头文件
读:
int txtread(const char* txtname)
{
ifstream CPULOAD_READ;
string strdata;
CPULOAD_READ.open(txtname, ios::in);
if (!CPULOAD_READ.is_open())
return -1;
char lineData[1024] = {0};
while(CPULOAD_READ.getline(lineData, sizeof(lineData)))
{
strdata += CharToStr(lineData);
strdata += "\n";
}
CPULOAD_READ.close();
return count(strdata.begin(),strdata.end(),'\n'); //统计行数
}
写:
float fValue = 0.0f;
char SendText[1000];
ofstream CPULOAD_ALL;
CPULOAD_ALL.open("CPULoad_all_1.txt", ios::out|ios::trunc); //把文件先清空再打开
//CPULOAD_HIGH.open("CPULoad_high_1.txt", ios::out|ios::app); //以追加的方式打开文件
if (!CPULOAD_HIGH.is_open())
return 0;
time_t timep;
time (&timep); //获取time_t类型的当前时间
char tmp[64];
strftime(tmp, sizeof(tmp), " %Y-%m-%d %H:%M:%S\n",localtime(&timep) );//对日期和时间进行格式化
sprintf_s(SendText,sizeof(SendText),"CPU Load:%3.0f%%", fValue);
CPULOAD_ALL << SendText<<" "<
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)