开发者

fseek not reading all the data?

开发者 https://www.devze.com 2023-02-20 10:11 出处:网络
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, wha

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 开发者_JAVA百科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;
}


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

精彩评论

暂无评论...
验证码 换一张
取 消