Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

More on FILE streams

Posted on 1998-09-18
5
Medium Priority
?
152 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
[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
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
ID: 1173046
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
ID: 1173047
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
ID: 1173048
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
ID: 1173049
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
ID: 1173050
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

  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 …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

719 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