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

  • Home
  • SEARCH
  • 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 6150111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:34:05+00:00 2026-05-23T19:34:05+00:00

I’m cleaning up some pretty complicated code that I didn’t write and I’m looking

  • 0

I’m cleaning up some pretty complicated code that I didn’t write and I’m looking for a way to touch the code as little as possible, so don’t flame me for what may appear to be a newb question:

I have a library that works with an external data file that can be written to or read from, but generally all the writes happen at once and all the reads. Internally, the FILE* is fopened with “r+b”, and the code appears to properly call fflush between switching between reads and writes. When the data file is in a place where the user has RW permissions, it works just as expected, however there are times where the data file may be in a location where the user has read-only permissions. Because of this the fopen(…”r+b”) fails and returns a NULL file pointer and bad things are happening. It’s totally reasonable for someone to have this data file in a read-only partition, they’re not required to update the file and should be able to use the file in a read-only situation.

My question is instead of doing

FILE* pFile=fopen("filename","r+b");

could I edit the code and do something like

FILE* pRead=fopen("filename","rb");
FILE* pWrite=fopen("filename","r+b");

Then in the code that reads from the file, just use pRead, and in the code that writes to the file use pWrite. This way I could do something like this

int UpdateTheFile()
{
   if (!pWrite) return 0; //we know that we shouldn't even try to write
   //change all the existing update code to use pWrite instead of pFile
   return 1;
}

int ReadFromTheFile()
{
   if (!pRead) return 0;
   ...
   return 1;
}

It seems wrong to me to have two file pointers to the same file, but since the code is already “correct” in its ability to flush between reads and writes now, I’m guessing that things may be kept in sync. Also, it’s guaranteed that only 1 thread will be accessing this file at a time, so I don’t need to worry about concurrency issues here.

Is this a really bad idea and should I think about properly switching between read-only and read-write in the proper functions with an fclose/fopen pair, or can I get away with this as a “quick fix”

  • 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-05-23T19:34:06+00:00Added an answer on May 23, 2026 at 7:34 pm
    int file_is_writable = 1;
    FILE *pFile = fopen("filename", "r+b");
    if (!pFile) {
      pFile = fopen("filename", "rb");
      file_is_writable = 0;
      /* I highly suggest you check for open failure here and do something sane */
    }
    

    Then check file_is_writable before updates.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.