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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:28:00+00:00 2026-05-21T02:28:00+00:00

I am making a c++ installer and I have appended both the file to

  • 0

I am making a c++ installer and I have appended both the file to extract and an 8 byte filesize of the file to extract within the program, to the executable. My program exits on a file read error, whats going wrong? To note I don’t have any knowledge about c file managing, apart from what I’ve learned today. I am writing the file test_before.tar.gz, which is 161 bytes, the executable is 12335 bytes long and the filesize file is 8 bytes long, containing 0000161. What’s wrong? Ask for more info if needed.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

int main(int argc, char* argv[])
{
    cout << "Opening the executable as read-only!" << endl;
    FILE *exeFile; // The executable file pointer
    FILE *outFile; // The file to write pointer

    // Check whether a file name was supplied
    if(argc < 2)
    {
        cout << "Please enter the file to write!" << endl;
        return 1;
    }

    // Open the executable as read-only
    if((exeFile = fopen(argv[0], "rb")) == 0)
    {
        cout << "Error opening the executable!" << endl;
        return 1;
    }

    cout << "Getting the executables size!" << endl;

    // Get the files size
    fseek(exeFile, 0, SEEK_END);
    int size = ftell(exeFile);

    cout << "Reading ofset!" << endl;   

    // Read the ofset bytes contained in the last 7-bytes
    char filesize_char[9];
    fseek(exeFile, -8, SEEK_END);
    fgets(filesize_char, 9, exeFile);

    // Convert
    int filesize = atoi(filesize_char);
    int ofset = (size - filesize) - 8;

    cout << "The ofset size is " << ofset << " bytes!" << endl;
    cout << "The file size is " << filesize << " bytes!" << endl;

    cout << "Reading the file to extract!" << endl;

    // Create the variable to contain the file and goto the ofset
    char* contents = new char[filesize + 1];
    fseek(exeFile, ofset, SEEK_SET);

    // Read the file to extract
    if(fread(contents, sizeof(char), filesize + 1, exeFile) != sizeof(contents))
    {
        // Error has occured
        if(feof(exeFile)) {
            cout << "Premature end of file!" << endl;

            // Delete variables so they dont "leak"
            fclose(exeFile);
            delete[] contents;

            return 1;
        } else {
            cout << "File read error!" << endl;

            // Delete variables so they dont "leak"
            fclose(exeFile);
            delete[] contents;

            return 1;
        }
    }

    cout << "Writing the file to " << argv[1] << "!" << endl;

    // Write the file to extract
    if((outFile = fopen(argv[1], "wb")) == 0)
    {
        cout << "Error opening the file to write!" << endl;

        // Delete variables so they dont "leak"
        fclose(exeFile);
        fclose(outFile);
        delete[] contents;

        return 1;
    }

    fwrite(contents, 1, sizeof(contents), outFile);

    //delete variables
    fclose(exeFile);
    fclose(outFile);
    delete[] contents;

    return 0;
}
  • 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-21T02:28:01+00:00Added an answer on May 21, 2026 at 2:28 am

    You don’t need the while loop at all. After all, you already allocated the memory that would contain all your data. Move file pointer to the start of data fseek(exeFile, ofset, SEEK_SET), then use fread to read it as whole, and then use fwrite to write it into outFile.

    You should open your exeFile and outFile with "rb" and "wb" flags otherwise your code would work reliably only with text data.

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

Sidebar

Related Questions

I'm making an installer for an OS X application, but have trouble understanding PackageMaker's
I get CLR20r3 error and very non-descriptive messages. I tried making an installer as
I'm making a program with c# and sql server and I have a problem
I'm making an installer that needs to have information stored inside the exe itself.
I have a Visual Studio Installer Project that I'm making in Visual Studio 2010
I am making an installer in java swing it almost completed only one thing
I am making my own installer and it's almost complete. The only thing lacking
At the end of my installer, I want to start an .exe file and
There are plenty of great answers to questions about making a standalone executable, but
I have installed Jenkins executable on OSX, but now I want to stop it

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.