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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:54:44+00:00 2026-05-25T15:54:44+00:00

I am opening a file with ifstream to check if it exists. Then I

  • 0

I am opening a file with ifstream to check if it exists. Then I close it and open it with ofstream to write to it, and I think setting ios::trunc flag allows me to overwrite it.

However I’d like the ability to keep the file open if it exists, but I used an ifstream to open it so does that mean I can’t write to the file till I close and re-open using fstream or ofstream? I didn’t use fstream to begin with because that wouldn’t tell me if the file was already there or not.

  • 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-25T15:54:44+00:00Added an answer on May 25, 2026 at 3:54 pm

    Just open a read-write fstream on the file. You can test if the file previously existed (and was non-empty) by seeking to the end and seeing if you’re at a non-zero offset. If so, the file existed, and you can do whatever with it. If not, the file didn’t exist or was empty. Assuming you don’t need to distinguish between those two cases, you can then proceed as if it did not exist.

    For example:

    // Error checking omitted for expository purposes
    std::fstream f("file.txt", std::ios::in | std::ios::out);
    f.seekg(0, std::ios::end)
    bool didFileExist = (f.tellg() > 0);
    f.seekg(0, std::ios::beg);
    
    // Now use the file in read-write mode.  If didFileExist is true, then the
    // file previously existed (and has not yet been modified)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm opening an istream for binary read: ifstream file; file.open (this -> fileName.c_str(), ios::binary);
void BinaryTree::InitializeFromFile(string Filename){ ifstream inFile; inFile.open(Filename, fstream::binary); if(inFile.fail()){ cout<<Error in opening file <<Filename; return;
I'm opening a file and finding the line I need, but then I have
Is there a way to check if a file exists (for now just on
When I use the std::ifstream to open a file that has been written in
I am trying to write a program that read a file using fstream then,
What I'm attempting to accomplish, is to open an external .dat file using ifstream
For performing file IO in C++ we use the ofstream, ifstream and fstream classes.
void BinaryTree::InitializeFromFile(string Filename){ ifstream inFile; treenode* Freq[256]; inFile.open(Filename.c_str(), fstream::binary); if(inFile.fail()){ cout<<Error in opening file
After opening a file created by root in vim , how to change the

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.