Solved

More on FILE streams

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 73
is twain_32.dll cmpatible with windows 10 ? 10 276
C++ :Change value from  DisableCMD registry 4 80
draw a Christmas tree by using a nested loop? 26 105
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

751 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