代码拉取完成,页面将自动刷新
#include "FileSave.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <windows.h>
#include <io.h>
#include <direct.h>
#include <string>
using namespace std;
int createDirectory(std::string path)
{
int len = path.length();
char tmpDirPath[256] = {0};
for (int i = 0; i < len; i++)
{
tmpDirPath[i] = path[i];
if (tmpDirPath[i] == '\\' || tmpDirPath[i] == '/')
{
if (_access(tmpDirPath, 0) == -1)
{
int ret = _mkdir(tmpDirPath);
if (ret == -1)
return ret;
}
}
}
return 0;
}
int get_last_num_from_log(char *log_name)
{
ifstream inFile;
inFile.open(log_name, ios::in);
if (inFile.good())
{
inFile.seekg(-1, ios_base::end); // go to one spot before the EOF
bool keepLooping = true;
while (keepLooping)
{
char ch;
inFile.get(ch); // Get current byte's data
if ((int)inFile.tellg() <= 1)
{ // If the data was at or before the 0th byte
inFile.seekg(0); // The first line is the last line
keepLooping = false; // So stop there
}
else if (ch == '\n')
{ // If the data was a newline
keepLooping = false; // Stop at the current position.
}
else
{ // If the data was neither a newline nor at the 0 byte
inFile.seekg(-2, ios_base::cur); // Move to the front of that data, then to the front of the data before it
}
}
string lastLine;
getline(inFile, lastLine); // Read the current line
inFile.close();
return atoi(&lastLine[0]);
}
else
{
inFile.close();
return 0;
}
}
void attach_new_log(char *log_name, int new_num)
{
ofstream outFile;
outFile.open(log_name, ios::app);
if (outFile.is_open())
{
SYSTEMTIME sys;
GetLocalTime(&sys);
outFile << endl
<< new_num << " " << sys.wYear << " " << sys.wMonth << " " << sys.wDay << " " << sys.wHour << " " << sys.wMinute << " " << sys.wSecond;
outFile.close();
}
}
void FileSave::construct_new_record(int new_num)
{
string record_name = "./data_serial/Record" + to_string(new_num);
record_name += ".txt";
cout << "record_name is " << record_name << endl;
record_file.open(record_name.c_str(), ios::out);
}
FileSave::FileSave()
{
cout << "FileSave" << endl;
char log_name[] = "./data_serial/log.txt";
createDirectory("./data_serial/");
last_num = get_last_num_from_log(log_name);
new_num = last_num + 1;
attach_new_log(log_name, new_num);
construct_new_record(new_num);
}
void FileSave::save_three_num(float a, float b, float c)
{
if (record_file.is_open())
{
record_file << a << " " << b << " " << c << " " << endl;
}
else
{
cout << "record_file error" << endl;
}
}
FileSave::~FileSave()
{
if (record_file.is_open())
{
record_file.close();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。