Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C file streams, appending at the beginning

Is there a simple way to append something at the beginning of a text file using file streams? Because the only way I can think of, is to load a file into a buffer, write text-to-append and then write the buffer. I want to know if it is possible to do it without my buffer.

like image 725
textman Avatar asked Jan 10 '11 22:01

textman


4 Answers

No, its not possible. You'd have to rewrite the file to insert text at the beginning.

EDIT: You could avoid reading the whole file into memory if you used a temporary file ie:

  1. Write the value you want inserted at the beginning of the file
  2. Read X bytes from the old file
  3. Write those X bytes to the new file
  4. Repeat 2,3 until you are done reading the old file
  5. Copy the new file to the old file.
like image 134
GWW Avatar answered Nov 10 '22 11:11

GWW


There is no simple way, because the actual operation is not simple. When the file is stored on the disk, there are no empty available bytes before the beginning of the file, so you can't just put data there. There isn't an ideal generic solution to this -- usually, it means copying all of the rest of the data to move it to make room.

Thus, C makes you decide how you want to solve that problem.

like image 5
Brooks Moses Avatar answered Nov 10 '22 12:11

Brooks Moses


Just wanted to counter some of the more absolute claims in here:

There is no way to append data to the beginning of a file.

Incorrect, there are, given certain constraints.

When the file is stored on the disk, there are no empty available bytes before the beginning of the file, so you can't just put data there.

This may be the case when dealing at the abstraction level of files as byte streams. However, file systems most often store files as a sequence of blocks, and some file systems allow a bit more free access at that level.

Linux 4.1+ (XFS) and 4.2+ (XFS, ext4) allows you to insert holes into files using fallocate, given certain offset/length constraints:

Typically, offset and len must be a multiple of the filesystem logical block size, which varies according to the filesystem type and configuration.

Examples on StackExchange sites can be found by web searching for 'fallocate prepend to file'.

like image 2
Foo Barian Avatar answered Nov 10 '22 12:11

Foo Barian


There is no way to append data to the beginning of a file.

The questioner also says that the only way they thought of solving the problem was by reading the whole file into memory and writing it out again. Here are other methods.

  1. Write a placeholder of zeros of a known length. You can rewind the file handler and write over this data, so long as you do not exceed the placeholder size.

    A simplistic example is writing the size of an unsigned int at the start that represents the count of lines that will follow, but will not be able to fill in until you reached the end and can rewind the file handler and rewrite the correct value.

    Note: Some versions of 'C' on different platforms insist you finally place the file handler at the end of file before closing the file handler for this to work correctly.

  2. Write the new data to a new file and then using file streams append the old data to the new file. Delete the old file and then rename the new file as the old file name. Do not use copy, it is a waste of time.

All methods have trade offs of disk size versus memory and CPU usage. It all depends on your application requirements.

like image 1
ztalbot Avatar answered Nov 10 '22 12:11

ztalbot