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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:43:07+00:00 2026-06-13T12:43:07+00:00

I am trying to write a program that read a file using fstream then,

  • 0

I am trying to write a program that read a file using fstream
then, rewrite some of the text and delete the rest of the file
This the code that I am trying to do

#include<iostream>
#include<fstream>

using namespace std;
int main(int argc, char **argv){
    fstream *binf;
    fstream someFile("t.txt", ios::binary|ios::out|ios::in);
    int i;
    for(i=0;i<3;i++){
        char c;
        someFile.seekg(i);
        someFile.get(c);
        cout<<"c:"<<c<<endl;
    }
    someFile.seekp(i++);
    someFile.put("Y");
    someFile.seekp(i++);
    someFile.put("Y");
    //Delete the rest of the file
    return 0;
}

Note the following flags for opening a file

ios::in Open for input operations.
ios::out    Open for output operations.
ios::binary Open in binary mode.
ios::ate    Set the initial position at the end of the file. If this flag is not set to any value, the initial position is the beginning of the file.
ios::app    All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations.
ios::trunc  If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.

I try many combinations of these but non of them help me to do what I want
I want to read the file until I find text. If I find the text that I want, I over write it and delete the rest of the file. So, the file should be re-sized to smaller file.

  • 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-13T12:43:08+00:00Added an answer on June 13, 2026 at 12:43 pm

    You can’t do that with single stream object.

    Possible solutions:

    Either close your file and call truncate function, :

     #include <unistd.h>
     int ftruncate(int fildes, off_t length);
     int truncate(const char *path, off_t length); 
    

    MS Windows version of truncate is _chsize – see http://msdn.microsoft.com/en-us//library/dk925tyb.aspx

    int _chsize( 
       int fd,
       long size 
    );
    

    Or open your file for reading only, read/replace to some stringstream, then put everything to your file this time opened for overwriting:

    fstream someFile("t.txt", ios::binary|ios::in);
    stringstream ss;
    // copy (with replacing) whatever needed from someFile to ss 
    someFile.close();
    someFile.open("t.txt", ios::binary|ios::out|ios::trunc);
    someFile << ss.rdbuf();
    someFile.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to write a program that reads from a text file into a
I am trying to write a program that will take a text file with
I'm trying to write a code that will read a file and make some
I am trying to read a binary file from a program that writes a
Hey there. I'm trying to write a small program that will read the four
I am trying to write a simple program in C# that will read data
I am trying to write a program that sends basic text files from one
i am trying to write a program that close explorer then runs another program.
I'm trying to write a secure transfer file program using Python and AES and
I am trying to write a c++ program that would read key frames from

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.