使用一个由c++修改的未关闭的文本文件安全吗
is it safe to use a text file that is modified by c++ and is not closed?
标题不太清楚,但我的意思是:
std::fstream filestream("abc.dat", std::ios::out);
double write_to_file;
while (some_condition) {
write_to_file = 1.345; ///this number will be different in each loop iteration
filestream.seekg( 345 );
filestream << std::setw(5) << write_to_file << std::flush;
///write the number to replace the number that is written in the previous iteration
system( "./Some_app ./abc.dat" ); ///open an application in unix,
////which uses "abc.dat" as the input file
}
filestream.close();
这是一个粗略的想法,每次迭代都将数字重新写入文件并刷新。我希望不要在每次迭代中打开和关闭文件,以节省计算时间。(也不确定打开和关闭的复杂性:/(这样做可以吗?
在unix上,std::flush
不一定会写入物理设备。通常不会。std::ofstream::flush
调用rdbuf->pubsync()
,后者又调用rdbuf->sync()
,后者又"将受控序列与数组同步"。这些"控制序列"是什么?通常,它们不是底层物理设备。在像unix这样的现代操作系统中,高级I/O结构之间有很多东西,比如C++的I/O缓冲区概念和设备上的位。
即使是低级POSIX函数fsync()
也不一定保证比特被写入设备。即使关闭和重新打开输出文件也不一定能保证位被写入设备。
你可能需要重新考虑你的设计。
在调用system
(但使用<< std::flush;
(之前,您至少需要用filestream.flush()
刷新C++流缓冲区
我假设./Someapp
不是在写文件,而是只为读取而打开它。
但在您的情况下,最好在每次迭代时打开和关闭文件,因为system
调用显然是一个巨大的瓶颈。
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- C++ 从文本文件中读取每两行,并且在矢量中是安全的
- 使用一个由c++修改的未关闭的文本文件安全吗