my program creates a file then copies a file into the file created then i can copy another file into the file created turning it into one bog file i am having troubles when coming to extracting the exact file, can anyone suggest anything on how about to achieving this.?


new = lseek ( descriptor, offsetBy, from)
int new;
the new value of the current position in the file
int descriptor;
the descriptor as returned when the file was opened;
int offsetBy;
the amount (positive or negative) by which the position is to be adjusted
int from;
where the adjustment is to be calculated from - one of the constants
SEEK_SET from start of file
SEEK_CUR from current position
SEEK_END from end of file

Recommended Answers

All 6 Replies

one of my ideas is when copying a file, create another file and add the sizeof the file the name and the position of the pointer.

What does your archive file format look like? A simple format would consist of records like so:

struct record {
    size_t size;
    unsigned char *bytes;
};

void write_record(FILE *out, struct record *rec)
{
    fwrite(rec->size, sizeof rec->size, 1, out);
    fwrite(rec->bytes, 1, rec->size, out);
}

int read_record(FILE *in, struct record *rec)
{
    if (fread(rec->size, sizeof rec->size, 1, in) == 1) {
        rec->bytes = malloc(rec->size);

        if (rec->bytes != NULL) {
            fread(rec->bytes, 1, rec->size, in);
            return 1;
        }
    }

    return 0;
}

From there it's a simple matter of appending each file to the archive file:

int archive_file(FILE *archive, FILE *source)
{
    struct record rec = {0};

    // Assuming get_file_size has been written
    rec->size = get_file_size(source);
    rec->bytes = malloc(rec->size);

    if (rec->bytes != NULL) {
        fread(rec->bytes, 1, rec->size, in);
        write_record(archive, &rec);
        free(rec->bytes);
        return 1;
    }

    return 0;
}

And pulling each archived file back in sequence:

void extract_all(FILE *archive)
{
    struct record rec;

    while (read_record(archive, &rec)) {
        FILE *dest = fopen(tmpnam(), "wb");

        if (dest != NULL) {
            fwrite(rec.bytes, rec.size, 1, dest);
            free(rec.bytes);
            fclose(dest);
        }
    }
}

All of this code is completely off the top of my head and written in the post text editor, so don't expect perfection. ;)

so basically you store the size of the file and the bytes, then you write this within the archive and when you extract the archive you just write out the size and the record of each file plus the destination

so you do not even need to store the record in a file?

in your extraction example would everything be extracted into "tmpnam"

so basically you store the size of the file and the bytes, then you write this within the archive and when you extract the archive you just write out the size and the record of each file plus the destination

All correct except the size isn't written when extracting. It's only used to determine how much actual data to extract.

so you do not even need to store the record in a file?

Records are stored in the archive file.

in your extraction example would everything be extracted into "tmpnam"

No, the tmpnam function returns a unique file name. In a more realistic program I'd include the name and permissions of the original file so that extraction produces the same thing that was archived.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.