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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:42:01+00:00 2026-05-30T06:42:01+00:00

#include <iostream> #include <fstream> int main() { std::ofstream outfile(text.txt, ios::trunc); std::ifstream infile(text.txt, ios::trunc); outfile.seekp(0);

  • 0
#include <iostream>
#include <fstream>

int main() {
    std::ofstream outfile("text.txt", ios::trunc);
    std::ifstream infile("text.txt", ios::trunc);

    outfile.seekp(0);

    std::cout << "This is a file";

    infile.seekg(0, ios::end);
    int length = infile.tellg();
    infile.read(0, length);

    infile.close();
    outfile.close();
    return 0;
}

I think I get the idea behind this, but I feel like (and I’m pretty sure) I have no idea what I’m doing. I’ve looked it up and everything has confused me. I’ve read through a C++ reference, and then I googled it, but I still don’t understand what I’m doing wrong.

#include <iostream>
#include <fstream>
#include <cstring>

int main() {
    std::fstream file("text.txt", std::ios_base::in | std::ios_base::out);

    file << "This is a file";
    int length = file.tellg();

    std::string uberstring;
    file >> uberstring;
    std::cout << uberstring;

    char *buffer = new char[length + 1];
    file.read(buffer, length);
    buffer[length] = '\0';

    file.close();
    delete [] buffer;

    return 0;
}

I tried this, but it isn’t printing anything. Why isn’t this working?

  • 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-30T06:42:02+00:00Added an answer on May 30, 2026 at 6:42 am

    If you want to read and write to the same file, just use a normal std::fstream … there is no need to attempt and open the same file as both a ifstream and ofstream. Also if you want to write data to the file, use the operator<< on the actual fstream instance object, not std::cout … that will simply write to wherever std::cout is set, which is typically the console. Finally, the call to read has to go back into a buffer, you can’t use NULL as an argument. So your code would change to the following:

    int main() 
    {
        std::fstream file("text.txt", ios_base::in | ios_base::out);
    
        //outfile.seekp(0); <== not needed since you just opened the file
    
        file << "This is a file"; //<== use the std::fstream instance "file"
    
        //file.seekg(0, ios::end); <== not needed ... you're already at the end
        int length = file.tellg();
    
        //you have to read back into a buffer
        char* buffer = new char[length + 1];
        infile.read(buffer, length); 
        buffer[length] = '\0'; //<== NULL terminate the string
    
        file.close();
        delete [] buffer;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<fstream> #include<iostream> #include<string> #include<algorithm> using namespace std; int main(){ ifstream infile; ofstream outfile; infile.open(oldfile.txt);
#include <iostream> #include <fstream> using namespace std; int main() { ifstream stream1(source.txt); string line
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open
#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
here is code #include <iostream> #include <fstream> #include <cstring> using namespace std; int main()
#include <iostream> #include <string> #include <fstream> using namespace std; int main() { char x[20];
This is my code: #include <iostream> #include <fstream> using namespace std; int main() {
#include<iostream> #include<fstream> #include<cstdlib> #include<string> using namespace std; **int main() { double write(); double read();

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.