Thread: File archiving

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    File archiving

    File archivingg

    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:
    Insert
    Code:
    #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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So first would be dealing with the command line options. Do you just want a bunch of filenames? If so, you'll have to loop based on argc to get all the files read.

    Then second, how are you going to denote in your archive when one file ends and the next begins?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    I understand I have to do a loop, most likely a for loop but I am really uncertain on how I would go on to doing this.

    I also think I need to have file headers, to separate 1 file from another but I tried looking for examples on how I would go on to creating file headers but i kept running into a dead end and it always came up with header files which really different from file headers.

    I would be really grateful if you can help me out, thanks a lot for your reply.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    For the first, do you know how for loops work? If so, what are you missing to add one here?

    As to the second, your belief that The One True Answer exists Out There if only you knew the right words to put in Google, while cute, is utterly false. You will have to think about the problem, devise a method, see if you can find problems with your method, overcome the flaws, lather, rinse, repeat.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2010, 12:05 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM