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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:07:58+00:00 2026-06-11T04:07:58+00:00

Using C++’s <fstream> , it’s pretty easy to copy a text file: #include <fstream>

  • 0

Using C++’s <fstream>, it’s pretty easy to copy a text file:

#include <fstream>

int main() {
    std::ifstream file("file.txt");
    std::ofstream new_file("new_file.txt");

    std::string contents;
    // Store file contents in string:
    std::getline(file, contents);
    new_file << contents; // Write contents to file

    return 0;
}

But when you do the same for an executable file, the output executable doesn’t actually work. Maybe std::string doesn’t support the encoding?

I was hoping that I could do something like the following, but the file object is a pointer and I’m not able to dereference it (running the following code creates new_file.exe which actually just contains the memory address of something):

std::ifstream file("file.exe");
std::ofstream new_file("new_file.exe");

new_file << file;

I would like to know how to do this because I think it would be essential in a LAN file-sharing application. I’m sure there are higher level APIs for sending files with sockets, but I want to know how such APIs actually work.

Can I extract, store, and write a file bit-by-bit, so there’s no discrepancy between the input and output file? Thanks for your help, it’s much appreciated.

  • 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-11T04:08:00+00:00Added an answer on June 11, 2026 at 4:08 am

    Not sure why ildjarn made it a comment, but to make it an answer (if he posts an answer, I will delete this). Basically, you need to use unformatted reading and writing. getline formats the data.

    int main()
    {
        std::ifstream in("file.exe", std::ios::binary);
        std::ofstream out("new_file.exe", std::ios::binary);
    
        out << in.rdbuf();
    }
    

    Technically, operator<< is for formatted data, except when use it like the above.

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

Sidebar

Related Questions

Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
using a binary search tree I need to add to a vector all int
Using Java,I have to fetch multiple sets of values from an XML file to
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I have a reasonable size flat file database of text documents mostly saved in
Using The Play Framework how easy would it be to allow an admin user
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using a populated Table Type as the source for a TSQL-Merge. I want to

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.