如何在 c++ 中的循环中执行 fwrite
How to perform fwrite in a loop in c++
好吧,这是我的代码似乎失败了,甚至没有至少写一次,我看不出到底出了什么问题,谁能指出我正确的方向?
我的目的是在无限循环中每 10 秒执行一次写入
FILE* pFile;
pFile = fopen("Hdd:\LOGFile.txt", "w");
while(true) {
DWORD TitleID = XamGetCurrentTitleId();
std::ostringstream titleMessageSS;
titleMessageSS << "Here's the current title we're on : " << TitleID << "r";
std::string titleMessage = titleMessageSS.str(); // get the string from the stream
DWORD dwBytesToWrite = (DWORD)titleMessage.size();
DWORD dwBytesWritten = 0;
fwrite(titleMessage.c_str(), 1 , dwBytesToWrite, pFile);
Sleep(10000);
}
fclose(pFile);
return NULL;
您可能应该在致电Sleep
之前致电fflush(pFile);
;但是你应该最好使用C++ std::ostream
(并使用std::flush
操纵器(。
你怎么知道它不写? 文件是否在程序停止。
我怀疑的是您在程序已停止。 流类型(C++ 和 C(通常为缓冲; fwrite
将数据复制到本地缓冲区,并且仅传输write
当缓冲区为已满,当您显式刷新它时(请参阅fflush
(或当文件已满时闭。 例如,这就是为什么我们必须检查输出错误的原因fclose()
后.
相关文章:
- 在映射擦除c++期间执行循环的次数
- C++-For循环未执行
- 如何强制循环执行另一次迭代到零并关闭循环
- 程序在循环执行后崩溃
- 如何使用两个 for 循环执行唯一的洗牌
- 对于循环执行失败,因为变量我不会递增,为什么?
- pi的倍数到千分之一的值可能会改变循环执行方式
- For 循环执行奇怪的增量跳转
- 在循环执行之间释放内存
- 如何计算循环执行了多少次?C++
- 给定范围内的完美平方:循环执行异常
- 需要澄清有关此循环执行乘法的信息
- 有条件的大平面数组遍历和令人惊讶的短循环执行时间
- c++openmp,用于循环执行期间的循环最大减少值
- 为什么循环执行时间的此C++存在显着差异?
- 对于循环条件内的循环执行,C++
- 使用for循环执行线程
- c++中While循环执行得太早
- c++中while循环执行中的打印输出语句
- 这个 while 循环执行其适当的功能,但对于其他内容是必需的