Solved

More on FILE streams

Posted on 1998-09-18
5
148 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

777 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