Solved

More on FILE streams

Posted on 1998-09-18
5
147 Views
Last Modified: 2010-04-01
Im back again,
Is it possible to REMOVE or REMOVE and REPLACE an entry from a stream (either a structure or text).
ie:
If I have saved 10 structures (of the same format) but would like to remove one of them.

or

if i have a few lines of text, I would like to remove a line or a word.
Thanx in advance
dsch
0
Comment
Question by:dsch
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
Read from the stream and write what you read to a new stream, but don't write out the things you want to delete.  Then use the new stream instead of the old.
0
 
LVL 2

Expert Comment

by:Paullkha
Comment Utility
Read the file and write a new one, Huh!

I can barely remember my CS courser, but here goes.
If the structure has a fixed size:
To Delete:
1.Open the file in random access
2.Read the last structure (structure #N)
3.Overwrite the structure you want to delete (structure #3)
4.Mark the last structure as open (deleted).
To Write:
1. Open the file in random access.
2. Traverse to the end.
3. Read backwards until you find the last open structure.

YOU DO NOT WANT TO READ A 50MEG FILE THAN WRITE OUT A 50MEG FILE TO DELETE 100KB OF DATA!

Even if the structure is not fixed, simply mark the structure as deleted. When the file contains say 30% invalid structures, than do as neitod says.

There are better ways than I what I have listed here. But college was too long ago.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I don't think you can do random access on a file stream.  (note stream, not just a regular file).
0
 
LVL 1

Author Comment

by:dsch
Comment Utility
Thankyou nietod & Paullkha for your comments.
I think that nietod's answer will suffice for now as my app's file stream will probably only be 100k max anyway.
>>I don't think you can do random access on a file stream.  (note stream, not just a regular file).
I will try to look this up in my documentation.
Also if you only "3.Overwrite the structure you want to delete (structure #3)" the file size will not be reduced will it? There will just be an empty space. Am I right?
Any way, I'll try reading, then writing and see  how I get on.
I may be back later.
Thanx again
dsch
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
There is no empty space in a file.  it is sort of like an array that has not specific size.  As you write to the array it continues to grow.  When you random access to the file (which is not possible with streams), you can make the file smaller, but removing part of the end of the file.  Thus with random access, you can shift things forward and then shrink the file to account for the items at the end that are no longer needed.  But this can't be done with streams.  With streams your only option is to copy the information to a different stream (doesn't have to be a file).  Then reopen the file stream specifying ios::trunc to clear its previous contents and then write the information back.  This is not very efficient, but it streams weren't designed with high speed performance in mind, then were designed for programmer ease.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now