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 9266027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:13:21+00:00 2026-06-18T14:13:21+00:00

I am trying to open a file for writing and reading simultaneously, in windows.

  • 0

I am trying to open a file for writing and reading simultaneously, in windows.
I have one program which writes (every one second) to the file and one that reads from it. In unix it works prefectly but it doesn’t work in windows (I can’t open an already opened file).
I open the file with fopen().

How can I solve this problem?

EDIT2:

check out _fsopen it uses FILE *, and set the share flag accordingly.

EDIT:

First of all, some code: this is how I used to open the file

   FILE* f = NULL;
        int res = fopen_s(&f, "c:\\temp\\File1.txt", "w");
        if (res != 0) return;

        while (true) {
            Sleep(1000);
            fprintf_s(f , "Some data");
        }
        fclose(f); 

The read was in other applicaiton, but it did fscanf instead.

The fixed code:

char d[] = "data";


HANDLE h = CreateFile("c:\\temp\\f.txt", GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, CREATE_ALWAYS, /*FILE_ATTRIBUTE_NORMAL*/ FILE_FLAG_WRITE_THROUGH, NULL);

if (h == INVALID_HANDLE_VALUE) return 0;

DWORD bytesW;
while(true) {
    Sleep(100);
        WriteFile(h, d, strlen(d), &bytesW, NULL);
}

CloseHandle(h);
return 0; 
  • 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-18T14:13:22+00:00Added an answer on June 18, 2026 at 2:13 pm

    Both Windows and Linux have a default way of opening a file, which fopen uses by default.

    In Windows, that means blocking (only one process can open a file at a time).

    In Linux, it means non-blocking.

    fopen is a high-level API. To choose yourself the blocking policy on the file, for Windows you should use OpenFile from WinAPI. In particular, have a look at the OF_SHARE_* flags.

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

Sidebar

Related Questions

I have a library that handles reading and writing a cache file. This library
I am trying to open a file which normally has content, for the purpose
I'm trying to open a file in a Java program (say Program.java ) using
I have two C# applications, one is reading a file (File A) line by
I am trying to make a program that writes the binary code of a
So, I've been trying to automatize binary file reading and writing in C++ in
I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel
I have been trying to create a Main program that calls another program, sending
I'm trying to open a file for input in Excel with this line :
I'm trying to open a file and read from the last point read. My

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.