Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 18th, 2006, 12:12 PM
Authorized User
 
Join Date: Aug 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shivanshub
Default

Hello friends.

What about my prob. My main motive is to reclaim free space from file. Like this, suppose we want to write a string in file say “SHIVANSHU”. It will take some space on storage media assume 5Kb now I want to delete some contents like “S” from file then after delete it will be “HIVANHU” and have size 3Kb. My problem is to reclaim this 2Kb but with out making temp copy of file. Because with big file it’s not efficient way. Pls tell me any in built class OR logic to do this.

 Thanks for ur valuable help.



B.Tech(Computer Science) final year from Chandigarh,INDIA
 
Old August 18th, 2006, 04:22 PM
Authorized User
 
Join Date: Aug 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First, what kind of file do you want to do this with? Binary, text etc..

I'am not an expert in file manipulation but you may explore the FileStream class. Streaming data one byte at a time is reasonably efficient.

Regards,
.NETamateur
Electrical Engineer
 
Old August 18th, 2006, 04:31 PM
Authorized User
 
Join Date: Aug 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shivanshub
Default

thanks 4 reply. my prob is not to write in file my is delete from file. dear
hope some one can ans one day....

I am not like every body else.
 
Old August 18th, 2006, 05:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

You either append to a file or re-write it. That’s all the modification you are allowed though the ‘official channel’.

If your files are gonna be of the size of a few 100 KBs or even a few MBs, however, you can read all the data in a buffer, modify the data in the buffer and overwrite the contents of your file with what you have in the buffer.

If your files are too big for that then there are things you can do like 'seek' to
'positions' in your file (using FileStream's Seek function) and write the data over there. But that 'replaces' the data at those positions (rather than inserting or deleting) and I don’t see how that will help you.

If these are flat data files your are talking about then you can resort to keeping index files, where you would maintain, in separate index files, the indexes of the actual data in your data files. For deletion you mark a certain indexes as deleted in the index files. You can periodically run batch processes to go through changes marked in the index files and carry them out in the data files.

But if you are seriously considering doing all that I would suggest you to also consider having a database file in place of that.

Finally if it’s a part of some sort of lab project where you have to demonstrate as a POC of in-place changes in a file then I would say nothing is impossible but you will have to indulge yourself in some pointer-magic, which requires a lot of work and testing on the programmer’s part.


Regards
Ankur
 
Old August 18th, 2006, 07:46 PM
Authorized User
 
Join Date: Aug 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, in your case you will delete some characters. I do not have the time to figure out how to that in an efficient way with the FileStream class but I am not surprised if it is possible. That's why I said explore the FileStream class. With a little bit of creativity it may be possible.

For example a simple and tricky way to remove file content (whole content) from a file efficiently with the FileStream class is as follows (you may try it with 1GB file if you want):

FileStream file_stream = new FileStream("path to file", FileMode.Open);
file_stream.SetLength(0);
file_stream.Close();



Regards,
.NETamateur
Electrical Engineer
 
Old August 19th, 2006, 09:41 AM
Authorized User
 
Join Date: Aug 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shivanshub
Default

Hello friends. Mission is over successfully with Ur help. So, first of all congratulation to all of u. friends. The thing was so simple but two ideas for this work together. One work as INDEX system and Second is “setlength” property. As I asking 4 how to delete the data from first day.

The story is like this I have the text file containing data (in form of object) and index file of that data. I wrote the data in file and entry of that data in index file which have flag for data. If data is exist the flag is true other wise false and two more entry start point of data and size of data. When I have to delete the data I set flag false by this data is deleted for application. But not from storage media. For that I have idea of defragmenter of that data as per entry in index file. This will set virtual deleted data at end. Now I will reset the length of file by property. Here the space is free by file and my problem is solved.

Great great great thanks for all of u for Ur valuable time and efforts.

Last problem as I new to this forum now pls tell me how to lock this topic.



I am not like every body else.
 
Old August 19th, 2006, 09:46 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:Last problem as I new to this forum now pls tell me how to lock this topic.
You can't. And you don't have to...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old August 19th, 2006, 09:48 AM
Authorized User
 
Join Date: Aug 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shivanshub
Default

ok thanks

I am not like every body else.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename web site, aspx, folder in VS 2005? cJeffreywang BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 July 9th, 2007 11:40 AM
How To rename and delete from file shivanshub .NET Framework 2.0 2 August 18th, 2006 11:53 AM
Rename File/Folder shivanshub General .NET 0 August 11th, 2006 01:57 PM
Rename file without time change surendran PHP How-To 1 July 26th, 2006 05:38 AM
How to create a folder in server and rename files pramos.21d ASP.NET 2.0 Basics 0 April 24th, 2006 05:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.