1

I would like to create a new file on a different machine in the network and write to it. I use CreateFile and WriteFile MSDN functions to create and write to the file.

The problem here is writes to the network disk are inconsistent during network failures

During long failures and local caching enabled, the handle becomes invalid and error code ERROR_BAD_NETPATH (53) is returned. So closing existing handle and reopening new handle results in loss of data in the cache.

Disabling local caching through the use of FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags cannot be used here because it slows down the writes

What is the best way to do network file writes? or is this network failure one off and this has to be a limitation?

Britto
  • 501
  • 1
  • 9
  • 22
  • One simple solution is to simply rewrite the file from the beginning, which could be tiresome on bad networks and 90% of a large file have been transferred. – Some programmer dude Nov 22 '19 at 17:28
  • Can you run a program on the target machine ? I'd put a "server" code on the receiver with a simple socket – Maskim Nov 22 '19 at 18:47
  • 2
    If you are encountering lots of network failures, you could consider breaking your writes into smaller chunks. In the event of a failed write, reopen the handle and continue appending to the file from the last failed chunk. – Larry B Nov 22 '19 at 18:49
  • Maxime.D not an option – Britto Nov 22 '19 at 18:55
  • 2
    I don't know the context, but It's not safe to write files remotely in that way. I recommend you to write the entire file in your local machine and then to copy it to the remote location. Other option is to have another program in the remote location and send the file content by TCP/IP – Marcelino Nov 22 '19 at 20:41
  • 1
    I would be surprised if remote uses of `WriteFile` and `CreateFile` isn't depending on TCP/IP. – Larry B Nov 22 '19 at 20:52
  • What type of file? Is this a text file (e.g., a log file) that you continually append to? Or is it some sort of structured file that gets updated chunks (either sequentially or random access)? – Adrian McCarthy Dec 09 '19 at 19:03
  • What's wrong with your network? Do you have the same problem reading files from the place where you now write them? – Ted Lyngmo Dec 10 '19 at 17:39
  • There's no magic bullet, cacheable goes with reliable. That's why these flags exist. It's like when you physically pull out an USB drive with write ahead enabled and w/o flushing the cache ("Eject"), you get corrupted files. – Simon Mourier Dec 11 '19 at 08:01

1 Answers1

1

do as Marcelino suggested, ie first write the file locally. Then use system() to run a copy utility program that works for your platform (eg: xcopy, or scp) as an external process. This functionality of reliably copying you should not try to duplicate. Just use it. :)

user2587106
  • 315
  • 1
  • 5