Hi,

Is there a possibility of creating fixed length file in C and when the file is filled up with the contents to the limit of it's size then it should wrap up to include the new contents?

Thanks.

Recommended Answers

All 3 Replies

Of course, it's possible (under your program "manual" contol). Open file in binary mode. Use fread and fwrite function. See fseek library function to set current write position and ftell to get it.

Create a file with all junk upto a maximum size:

x x x x x x x x x x
x x x x x x x x x x 
x x x x x x x x x x 
x x x x x x x x x x 
x x x x x x x x x x
x x x x x x x x x x

Now, whenever you want to write something in it, use the file pointers manipulators (as told by ArkM, the fseek) to place the write pointer to the particular location and then write your data:

a b c d e x x x x x
x x x x x x x x x x 
x x x x x x x x x x 
x x x x x x x x x x 
x x x x x x x x x x
x x x x x x x x x x

Of course, you will have to find divider which can differentiate between the junk and the good data.
I recommend that you store the size of the of the useful data at the beginning of each file:

6 a b c d e x x x x 
x x x x x x x x x x 
x x x x x x x x x x 
x x x x x x x x x x 
x x x x x x x x x x
x x x x x x x x x x

All these thing will work for Binary files at a particular implementation only.

You can create a new program for it where in you can declare a character array

char file[<file_size you want to set>];

Read in the file into this array,so you are limiting the size of it,then delete the contents of the file and write the contents of the array back to the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.