Read / write files in C

I am writing a C program that basically creates an archive file for a given list of file names. This is very similar to the ar command on Linux. Here's what the archive file would look like:

 !<arch> file1.txt/ 1350248044 45503 13036 100660 28 ` hello this is sample file 1 file2.txt/ 1350512270 45503 13036 100660 72 ` hello this is sample file 2 this file is a little larger than file1.txt 

But I am having difficulty extracting the file from the archive. Say the user wants to extract file1.txt . The idea is that it should have an index / location of the file name (in this case file1.txt), skip 58 characters to reach the contents of the file, read the content and write it to a new file. So here are my questions:

1) How can I get the index / location of the file name in the archive? Please note that duplicate file names are NOT allowed, so I don’t have to worry about having two different indexes.

2) How to skip a few characters (in this case 58) when reading a file?

3) How can I understand when the contents of a file end? that is, I need it to read the contents and stop right in front of the file2.txt/ header.

+7
source share
2 answers

My approach to solving this problem:

To have header information that contains the size of each file, its name and its location in the file.

Then parse the header, use fseek() and ftell() , as well as the fgetc() or fread() functions to get the bytes of the file, then create + write this data. This is the easiest way I can think of.

http://en.wikipedia.org/wiki/Ar_(Unix)#File_header <- The header of ar archives.

Example: @ programmer93 Consider that your header is 80 bytes long (the header contains metadata for the archive file). You have two files: one of 112 bytes and the other of 182 bytes. Now they are laid out in a flat file (archive file). So it will be 80 (header) .112 (file1.txt) .182 (file2.txt) .EOF. That way, if you know the size of each file, you can easily go (using fseek ()) to a specific file and extract only that file. [for extracting file2.txt I just fseek(FILE*,(112+80),SEEK_SET); and then fgetc () 182 times. I think I get it?

+3
source

If the file format cannot be changed by adding additional header information to help, you will have to look for it and work on how you go.

It should not be too complicated. Just read the file and when you read the title bar for example

 file1.txt/ 1350248044 45503 13036 100660 28 ` 

You can check the file name and size, etc. (you know that you will have a title bar at the beginning after !<arch> ). If this is the desired file, the ftell() function from stdio.h will tell you exactly where you are. Since the file size in bytes is specified in the header line, you can read the file by first reading this specific number of bytes in the usual order. Similarly, if this is not the file you want, you can use fseek() to move the number of bytes in the skipped file and be prepared to read in the header information for the next file and repeat the process.

+1
source

All Articles