?
Solved

Wrting to a file

Posted on 1998-12-02
7
Medium Priority
?
274 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:dooley090698
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1179097
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.


0
 

Author Comment

by:dooley090698
ID: 1179098
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.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1179099
>> 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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Answers2000
ID: 1179100
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] ;

:-).
0
 
LVL 22

Expert Comment

by:nietod
ID: 1179101
Right, I swore there was a "don"t in there.  Like "you don't need to read...."
0
 

Accepted Solution

by:
corneil earned 200 total points
ID: 1179102
If the order of the data in the file is not that important but you need quick access you should consider building an structure that will act like a table of contents so that you can access data anywhere in the file quickly and add new data to the end. As the TOC grows you add new blocks to the end with a pointer to the new block in the previous TOC block. This way you will not need to rewrite the file every time you add data. You can sort and rewrite your TOC but because the size does not change you will only overwrite existing TOC blocks.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1179103
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."
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question