Link to home
Start Free TrialLog in
Avatar of dooley090698
dooley090698

asked on

Wrting to a file

I want to insert data into a file and was wondering if there is an easier way other than reading all the data into a linked list of structures , inserting new data into the list then rewriting all the data back out again.
Am using BC++5.02 and OWL.
Txs
Avatar of Answers2000
Answers2000

You only need to read the stuff after the insertion point...For a simple sequential file format, there's no magic solution to the insertion problem.

Actually a better idea would be to implement the link list (or other complex data structures) on disk...instead of pointers to the next element, use file offsets.


Avatar of dooley090698

ASKER

Thanks for quick response.
I was afraid there weren't any easy solutions!
Problem being its a comercial software package so I can't change file layouts.
Just have to suffer  I guesse.

>> You only need to read the stuff after the insertion point
Don't you mean "before"?  i.e. read the part of the file that is after the insertion point into a buffer.  write out the new data at the insertion point.  Write the data saved in the buffer after the new data.

Note you don't necessarily need to use linked list etc to do this.  If the format is simple enough, this is just a matter of reading and writting arrays of bytes.
Well it depends on the file format, but I meant _after_ :

1. Skip the start of the file,
2. save the current file position into a variable
3. Read the end part of the file into a buffer
4. Seek back to saved file position from 2
5. Write the new data to insert
6. Write the buffer

What's the best data structure to mimic the file in memory obvious depends on the format.    Actually I find that for a lot of text based formats (configuration files, etc.) maps work pretty well - but that actually requires you parse the file - which may or may not be necessary depending on what you do.

If you know the file is fairly small (esp. in using Unix or Win32) just allocate/declare a big buffer, read the whole thing into memory, then rewrite it how you want, a char array is a simple quick'n'dirty way to do this.

I won't mention the program, but I do know of one commercial Unix program (I did not write it) which begins

int main( int argc, char * argv[] )
{
char buffer[1500000] ;

:-).
Right, I swore there was a "don"t in there.  Like "you don't need to read...."
ASKER CERTIFIED SOLUTION
Avatar of corneil
corneil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
corneil, please read the question history before answering.  (look who's talking--I read it--just wrong.)  dooley says "Problem being its a comercial software package so I can't change file layouts."