Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read/Write files in C

Tags:

c

unix-ar

I'm writing a program in C that basically creates an archive file for a given list of file names. This is pretty similar to the ar command in linux. This is how 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'm having difficulties trying to exract a file from the archive. Let's say the user wants to extract file1.txt. The idea is it should get the index/location of the file name (in this case file1.txt), skip 58 characters to reach the content of the file, read the content, and write it to a new file. So here's my questions:

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

2) How can I skip several characters (in this case 58) when reading a file?

3) How can I figure out when the content of a file ends? i.e. I need it to read the content and stop right before the file2.txt/ header.

like image 759
PoweredByOrange Avatar asked Oct 18 '12 21:10

PoweredByOrange


People also ask

How do you read and write in a file C?

In order to read information from a file, or to write information to a file, your program must take the following actions. 1) Create a variable to represent the file. 2) Open the file and store this "file" with the file variable. 3) Use the fprintf or fscanf functions to write/read from the file.

How do I read a text file in C?

In order to read from or write to simple text files, three steps are needed: Open the file using fopen. Read from the file using fscanf or write to the file using fprintf. Close the file using fclose.

What is writing a file in C?

Writing a File Following is the simplest function to write individual characters to a stream − int fputc( int c, FILE *fp ); The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error.

What does read () in C do?

The read() function reads data previously written to a file. If any portion of a regular file prior to the end-of-file has not been written, read() shall return bytes with value 0. For example, lseek() allows the file offset to be set beyond the end of existing data in the file.


2 Answers

My approach to solving this problem would be:

To have a 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 fgetc() or fread() functions to get bytes of the file and then, create+write that data to it. This is the simplest way I can think of.

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

EXAMPLE: @programmer93 Consider your header is 80 bytes long(header contains the meta-data of the archive file). You have two files one of 112 bytes and the other of 182 bytes. Now they're laid out in a flat file(the archive file). So it would be 80(header).112(file1.txt).182(file2.txt).EOF . Thus if you know the size of each file, you can easily navigate(using fseek()) to a particular file and extract only that file. [to extract file2.txt I will just fseek(FILE*,(112+80),SEEK_SET); and then fgetc() 182 times. I think I made myself clear?

like image 198
Aniket Inge Avatar answered Oct 15 '22 02:10

Aniket Inge


If the format of the file cannot be changed by adding additional header information to help, you'll have to search through it and work things out as you go.

This should not be too hard. Just read the file, and when you read a header line such as

file1.txt/      1350248044  45503 13036 100660  28        `

you can check the filename and size etc. (You know you'll have a header line at the start after the !<arch>). If this is the file you want, the ftell() function from stdio.h will tell you exactly where you are in the file. Since the file size in bytes is given in the header line, you can read the file by reading that particular number of bytes ahead in the normal manner. Similarly, if it is not the file you want, you can use fseek() to move forward the number of bytes in the file you are skipping and be ready to read in the header info for the next file and repeat the process.

like image 34
kallikak Avatar answered Oct 15 '22 02:10

kallikak