注意:写入启动时,如果发现日志文件大小没有改变,文件夹中的刷新,刷新没刷出来,就等等再刷新,也可以打开文件,会发现已经写入,打开文件不会影响文件的写入,只是不会文件实时刷新,重新打开,会发现追加写入成功!
参考链接:c++ 写入文件不频繁打开_C/C++编程笔记:你不知道的windows保存文件的坑_不妧的博客-CSDN博客
使用CreateFile读写文件_Tut-CSDN博客_createfile
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <Windows.h>
using namespace std;
void main()
{
HANDLE hfile = CreateFileA("H:\\1.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
if (SetFilePointer(hfile, 0, NULL, FILE_END) == -1)
{
cout << "SetFilePointer Error!" << endl;
return;
}
char buff[256] = "5666666666666666666666666666666666\n";
DWORD dwrite;
while (1)
WriteFile(hfile, &buff, strlen(buff), &dwrite, NULL);
CloseHandle(hfile);//关闭文件
}