The purpose of this program is to continue working with text files and binary fi
ID: 3807207 • Letter: T
Question
The purpose of this program is to continue working with text files and binary files. The goal is to implement a file archiving tool Background and overview An archiver is a tool for combining a collection of individual files into a single file for storage and portability. The resulting output file is called an archive. Some archivers only combine the files into one, while others will compress the result (ours will not do compression). The Unix tar com- mand is an example of an archiving tool (archives have the extension .tar). Another example is the collection of tools that generate zip files (these tools do archiving and icon). compression The program you implement will have the functionality to do each of the following: 1. Given a list of files and the desired archive name, create the archive while leaving the original files intact. 2. Given an archive file, create the individual files contained in that archive, while leaving the archive intact. 3. Given an archive file, list various statistical information about the file. 4. Given an archive file and a list of individual files, verify that the archive contains those files.Explanation / Answer
Hi Sample code for the above query looks like below i.e.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Blength 2046
struct Data
{
char FileName[255];
FILE *fp;
int length;
int nlength;
int slength;
struct Data *Nxt;
};
struct Data *Head = NULL, *current = NULL;
int getlength(FILE *fp)
{
int length = 0;
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
fseek(fp, 0L, SEEK_SET);
return length;
}
int add(char *FileName)
{
FILE *fp;
char Temporary[255];
struct Data *Nxt1;
Nxt1 = (struct Data *)malloc(lengthof(struct Data));
strcpy(Nxt1->FileName, FileName);
fp = fopen(FileName, "r+");
Nxt1->fp = fp;
Nxt1->length = getlength(fp);
Nxt1->nlength = strlen(FileName);
sprintf(Temporary, "%d", Nxt1->length);
Nxt1->slength = strlen(Temporary);
Nxt1->Nxt = NULL;
printf("File %s is being processed... ", FileName);
if(Head == NULL)
{
Head = Nxt1;
current = Nxt1;
}
else
{
current->Nxt = Nxt1;
current = Nxt1;
}
}
int preproc(int argumentc, char** argumentv)
{
int i;
for(i = 1; i <= argumentc-2; i++)
{
add(argumentv[i]);
}
}
int main(int argumentc, char** argumentv)
{
char block[Blength];
char stlength[5];
char shlength[100];
int rlength = 0;
int tnlength = 0, tslength = 0, hlength = 0, scount = 0;
struct Data *pointer;
FILE *fpar, *fp;
//HEADER CREATION
printf("Pre-processing the files ");
preproc(argumentc, argumentv);
printf("Completed.Preprocessing ");
printf("Header information.compiling ");
fpar = fopen(argumentv[argumentc-1], "w+");
pointer = Head;
while(pointer != NULL)
{
tnlength += pointer->nlength;
tslength += pointer->slength;
pointer = pointer->Nxt;
scount +=2;
}
hlength = tnlength+tslength+scount+1;
printf("Length of file FileNames is %d ", tnlength);
printf("Length of file lengths is %d ", tslength);
printf("Length of header except file length is %d. ", hlength);
sprintf(shlength, "%d/", hlength); //10 bytes of header length
fwrite(shlength, 1, strlen(shlength), fpar);
pointer = Head;
while(pointer != NULL)
{
fwrite(pointer->FileName, 1, pointer->nlength, fpar);
fwrite("/", 1, 1, fpar);
sprintf(stlength, "%d", pointer->length);
fwrite(stlength, 1, pointer->slength, fpar);
fwrite("/", 1, 1, fpar);
pointer = pointer->Nxt;
}
printf("header creation written to archieve file. ");
//BODY CREATION
pointer = Head;
while(pointer != NULL)
{
fp = pointer->fp;
while(rlength = fread(block, 1, lengthof(block), fp))
{
fwrite(block, 1, rlength, fpar);
}
pointer = pointer->Nxt;
}
printf("All files written content to archieve file. ");
fclose(fpar);
return 0;
}
Please let us know if you need anything else.Thanks!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.