简体   繁体   English

C ++ ifstream在写入时尝试打开文件

[英]C++ ifstream tries to open file while being written

I am polling a directory constantly for files and every time I see a file that meets some certain criteria for reading, I open the file and parse it. 我不断地为一个目录轮询文件,每当我看到一个符合某些阅读标准的文件时,我打开文件并解析它。

string newLine;

ifstream fileReader;

fileReader.open(filename.c_str());

while(!fileReader.eof())
{
getline(fileReader, newLine);
    // do some stuff with the line...
}
filereader.close();

The above code is in a loop that runs every 1 second checking a directory for new files. 上面的代码在一个循环中,每1秒运行一次,检查新文件的目录。 My issue is that as I am transferring files into the folder for processing, my loop finds the file and passes the name of the file to ifstream who then opens it and tries to parse an incomplete file. 我的问题是,当我将文件传输到文件夹进行处理时,我的循环找到文件并将文件名传递给ifstream然后打开它并尝试解析一个不完整的文件。 How do I make ifstream wait until the file is done being written before it tries to parse the file? 在尝试解析文件之前,如何让ifstream等到文件写完之后?

EDIT: Wanted to better word the issue here since a replier seems to have misunderstood my issue. 编辑:由于回复者似乎误解了我的问题,所以想在这里更好地说出这个问题。 I have 2 directories 我有2个目录

mydirectory/
mydirectoryParsed/

THe way my code works is that my program checks for files in mydirectory/ and when it finds them, parses them and uses the information in the files. 我的代码工作方式是我的程序在mydirectory /中检查文件,当它找到它们时,解析它们并使用文件中的信息。 No writing to the files are done. 没有写入文件。 Once I am done parsing the file, the file is moved to mydirectoryparsed/ 解析完文件后,文件被移动到mydirectoryparsed /

The issue is that when I transfer files over the network into mydirectory/ the ifstream sees these files midtransfer and starts reading them before they finish writing to the directory. 问题在于,当我通过网络将文件传输到mydirectory / ifstream看到这些文件传输时,并在完成写入目录之前开始读取它们。 How do I make ifstream wait until the file is completely written before parsing it? 在解析文件之前,如何使ifstream等到文件完全写入?

Don't transfer the files directly into the directory that your program is watching; 不要将文件直接传输到程序正在观看的目录中; instead, transfer them into a different directory on the same drive, and then when the transfer is done, move them into the watched directory. 相反,将它们转移到同一驱动器上的不同目录中,然后在传输完成后,将它们移动到监视目录中。 That way, the complete file appears in the watched directory in a single atomic operation. 这样,完整文件在单个原子操作中出现在监视目录中。

Alternatively, you could use a naming convention in the watched directory — append a suffix like ".partial" to files that are being transferred, and then rename the file to remove the suffix when the transfer is done. 或者,您可以在监视目录中使用命名约定 - 将后缀(如“.partial”)附加到正在传输的文件,然后重命名该文件以在传输完成时删除后缀。 Have your program ignore files whose names end with the suffix. 让程序忽略名称以后缀结尾的文件。

You're not supposed to open the file every time you write in it. 每次写入文件时都不应该打开文件。 Open it once! 打开一次!

Some pseudo-code for this would be : 一些伪代码将是:

 1- Open file 2- Get the data you want to write, treat that data 3- Call the write to file function 4- Loop until you have nothing left to write 5- Close de file 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM