Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:13:52+00:00 2026-06-17T23:13:52+00:00

I have two programs that read and write to the same file. One appends

  • 0

I have two programs that read and write to the same file. One appends data, the other clears the file and then rewrites all data.

The two programs interact fine on the same computer but when I run one on another computer and open the file over my local network my append doesn’t seem to get through in time.

My process is as follows:

Program 1:

  • Open file with

    handle = CreateFile(str.c_str(),
             FILE_READ_DATA|FILE_APPEND_DATA,
             0,
             NULL,
             OPEN_ALWAYS,
             FILE_ATTRIBUTE_NORMAL,
             NULL);
    
  • Write data using:

    WriteFile(handle, line, strlen(line), &Written, NULL);
    
  • Close file using:

    CloseHandle(handle);
    

Program 2:

  • Open file with:

    handle = CreateFile(str.c_str(),
             FILE_READ_DATA|FILE_WRITE_DATA,
             0,
             NULL,
             OPEN_ALWAYS,
             FILE_ATTRIBUTE_NORMAL,
             NULL);
    
  • If file size is different than last time it was read using:

    size = GetFileSize(handle, &size_high);
    

    (I’m only looking at size here, size_high is ignored since file sizes are relatively small)) then close the file, open it using FILE_READ_DATA flag, reload data and then retry from previous step.

  • Otherwise clear file data using:

    SetFilePointer(handle,0,NULL,FILE_BEGIN); 
    SetEndOfFile(handle); 
    
  • Rewrite all data using:

    WriteFile(handle, line, strlen(line), &Written, NULL);
    
  • Close file using:

    CloseHandle(handle)
    

Program 1 prompts the user to retry the save if the file is locked by another program.
Program 2 retries a couple of times if the file is locked by another program.

It looks like when accessing the file over the network my append operation in program 1 is not being picked up by program 2 before it clears and rewrites its data. I’ve tried opening the file in program 1 with FILE_FLAG_WRITE_THROUGH flag set and also using FlushFileBuffers(handle) before closing the file with no luck.

Is there something I’m missing in this process?

I am coding in Embarcadero C++ Studio if that helps.

Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:13:54+00:00Added an answer on June 17, 2026 at 11:13 pm

    File locking is known not to work reliably and robustly for file access to network shares. Simply put, you need to find another mechanism to implement mutual exclusion. For example, use a client/server database design.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two programs working together to write and read data from a file.
I have two (UNIX) programs A and B that read and write from stdin/stdout.
I have two programs- First one being a C program that writes in to
I need an XML-serializable dictionary. Actually, I now have two quite different programs that
I am developing two java programs that run in separate VM's that have a
I'm going to have two independent programs (using SqlAlchemy / ORM / Declarative) that
I have two scripts that are called in one program, the first script creates
I wanted to write a program that test if two files are duplicates (have
I am developing in python a file class that can read and write a
I have two simple programs that use named pipes, here is the first: int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.