0

I have a slight problem. I have a project that I'm working on and that requires two programs to read/write into some .txt files.

Python writes into one .txt file, C++ reads from it. C++ does what it needs to do and then writes its own information into another .txt file that Python has to read.

What I want to know is how can I check with C++ if Python has closed the .txt file before opening the same file, as Python may still be writing stuff into it and vice versa?

If you need any extra information about this conundrum, feel free to contact me.

R. Sepp
  • 21
  • 1

3 Answers3

1

whenever, in python, you use:

f.open()

always follow it with

f.close()

then you know its closed

see:

https://docs.python.org/2/tutorial/inputoutput.html

https://www.tutorialspoint.com/python/file_close.htm

re: comment

ETA

How to check if a file has been opened by another application in C++?

Is there a way to check if a file is in use?

C/C++ Standard Function to Check if a file is used by another process?

Way to check in C/C++ if a file is in use?

albeit hacky, I think my favorite after reading through was this one:

  if (  0 != rename("c:/foo.txt", "c:/foo.txt")  ) {
     printf("already opened\n");
  }

https://stackoverflow.com/a/1048721/3680588

Community
  • 1
  • 1
litepresence
  • 3,109
  • 1
  • 27
  • 35
  • 1
    Ok, but how will the C++ program know when `open` has been called by the Python program, and `close` has not yet been called? – Kevin Apr 02 '17 at 13:56
  • 1
    @Kevin a variable, maybe a bool, would probably be used to keep track of open and close, if you're doing this between programs maybe you can create a connection between the two using, for example, another file or a local internet connection, maybe you could have one program write to the others input file descriptor. I'm sure there are better ways though – Andria Apr 03 '17 at 01:10
0

You might consider having each "writer" process write its output to a temporary file, close the file, then rename it to the filename that the "reader" process is looking for.

If the file is present, then the respective reader process knows that it can read from it.

Phil Brubaker
  • 1,257
  • 3
  • 11
  • 14
-1

Using the with open() method always closes the file after operations are performed. See section 7.2 in the Input and Output documentation.

with open(in_file, 'w') as f:
    content = 'Example text'
    f.write(content)

Once the above is complete, the file is closed.

pstatix
  • 3,611
  • 4
  • 18
  • 40
  • This is not what the question asks. – shad0w_wa1k3r Apr 02 '17 at 14:11
  • Question technically asked for a C++ solution to determine if source file was closed. The above solution is a method to ensure the file operations are done prior to the C++ solution reading the file. OP asked how to know if the Python file is closed before opening it. If the operations posted are completed and the 'with open()' method has exited, the file is closed and ready to be open and read. – pstatix Apr 02 '17 at 14:23