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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:39:33+00:00 2026-06-03T21:39:33+00:00

I have a problem about file open mode with fopen() . In my case,

  • 0

I have a problem about file open mode with fopen().

In my case, I want to seek the cursor freely, may be even beyond EOF sometimes. What’s more, I also want to append to it instead of truncating the existing file. I tried to open the file in a+ mode; however, I cannot seek file cursor freely. Each time I seek the cursor beyond EOF, new arrival data will be append to end of the file, not the position I specified. While, if open in w+ mode, the existing file will be truncated. Is there a perfect solution to this problem?


UPDATE:

One point not clearly specified is that the file may not always exist; I have to create a new file in that case.

In fact, I want to process a configuration file. I don’t know whether it is good practice to implement like this. Or should I first place an empty configuration file. Thus there is no need to care about the case file does not exist?

Below is the code snippet:

FILE *f = fopen(FILE_PATH, "wb+");
struct record r;
if (f) {
    if (fread((void *)&r, 1, sizeof(struct record), f) {
        /* File exists, do the normal flow */
    } else {
        if (feof(f)) {
            /* File is newly created, do some initialization */
        }
    }
} else {
    /* issue a warning */
}
  • 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-03T21:39:35+00:00Added an answer on June 3, 2026 at 9:39 pm

    You will have to deal with the possibly non-existent file in two stages, first assuming that it is there and then dealing with its absence:

    if ((f = fopen(filename, "rb+") == 0)
        f = fopen(filename, "wb+");
    if (f == 0)
        ...report error...
    

    The "rb+" mode will fail to open a non-existent file (but otherwise behaves as you want). If the file doesn’t exist, then "wb+" will do what you want instead (though it could still fail, for example if the file exists but you don’t have permission to write to it). You have to hope that you’ve not been subjected to a TOCTOU (Time of Check, Time of Use) attack with the double attempt.

    An alternative approach uses the 3-argument version of the open() system call with appropriate flags to open a file descriptor, and then uses fdopen() to create a file stream from the file descriptor:

    #include <fcntl.h>
    
    int fd;
    if ((fd = open(filename, O_RDRW | O_CREAT, 0644)) >= 0)
        f = fdopen(fd, "rb+");
    

    You get rather precise control over open() with the flags.

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

Sidebar

Related Questions

I have a problem about div position relative alignment. I want the second div
I am using MFC CFile Seek function. I have a problem about Seek out
i have a problem. i want to get info about my string have other
I have following problem. I want to create a file system based session storage
I have a little problem about using jQuery (I really do not know jQuery
I have problem adding arraylist to list view, will explain about my problem here..
I have got a strange problem about in_array recently which I cannot understand. e.g.
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
This is more thinker than problem. I have a problem with thinking about how
So I have a problem that I don't really know how to go about.

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.