Jump to content

File archiving

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Tonyy

Tonyy

    Newbie

  • Members
  • Pip
  • 4 posts
Hi, I hope someone can help me with a problem that I am currently stuck on, basically I am trying to create a file archive utility which can archive 1 or more file each specified in a command line into a file.

My problem which I am currently stuck on is that, i have managed to create a file archive utility which can only archive 1 file.

e.g: My program can copy the contents of 1 file and put the contents of that file into a new file so, it can copy a file and its contents called hello and put that in a new file which can be named hello1, whatever the user wants to name the file.

I was also hoping if anyone can give me a clue on how to extract all of the files contained in the archive (specified on the command line) in to the current working
directory.


My code so far is:

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <fcntl.h>

#include <stdlib.h>


main (int argc, char **argv)

{

int bytes_read, bytes_written;

struct stat inode;

int input_fd, output_fd;

char buffer[64];

int eof = 0;

int i;


/* Check the command line arguments */

if (argc != 3)

{

printf("syntax is: %s <fromfile> <tofile>\n", argv[0]);

exit (1);

}


/* Check the input file exists and is a file */

if ((stat(argv[1], &inode) == -1) || (!S_ISREG(inode.st_mode)))

{

printf("%s is not a file\n", argv[1]);

exit(2);

}


/* Check that the output file doesnt exist */

if (stat(argv[2], &inode) != -1)

{

printf("Warning: The file %s already exists. Not going to overwrite\n", argv[2]);

exit(2);

}


/* Open the input file for reading */

input_fd = open(argv[1], O_RDONLY, 0);

if (input_fd == -1)

{

printf("%s cannot be opened\n", argv[1]);

exit(3);

}


output_fd = open(argv[2], O_CREAT | O_WRONLY | O_EXCL , S_IRUSR|S_IWUSR);

if (output_fd == -1)

{

printf("%s cannot be opened\n", argv[2]);

exit(3);

}


/* Begin processing the input file here */

while (!eof)

{

bytes_read = read(input_fd, buffer, sizeof(buffer));


if (bytes_read == -1)

{

printf("%s cannot be read\n", argv[1]);

exit(4);

}


if (bytes_read > 0)

{

bytes_written = write(output_fd, buffer, bytes_read);

if (bytes_written == -1)

{

printf("There was an error writing to the file %s\n",argv[2]);

exit(4);

}


if (bytes_written != bytes_read)

{

printf("Devistating failure! Bytes have either magically appeared and been written or dissapeard and been skipped. Data is inconsistant!\n");

exit(101);

}

}

else

{

eof = 1;

}

}

close(input_fd);

close(output_fd);


}

I'm hoping someone can help me with this.

Thanks a lot.
Tony

Edited by Roger, 10 January 2011 - 06:35 PM.
added code block


#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
If im not mistaken this code is, C not C#. I would try the C/C++ section over the C# section. ;)

~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users