Delete string within file

azae

Member
Joined
Jan 6, 2005
Messages
18
Hi All,

I have manage to do the read file and write to file.

But in C#.NEt, do we have delete specific string in the file?
To be frank, I only done file delete in other language like C.
I never done anything in delete string within file.

I would be appreciated if someone could tell me how to do it C#,C and perhaps VB.

Thanks :)>-
 
You cant delete it. A file can be thought of as an ordered set of bytes. If you want to get rid of a portion in the middle then you need to overwrite that portion with the remainder of the file and set the file size accordingly.
If the file is n bytes long and the string you wish to remove is at positon x and is m bytes long then:
Code:
int nBytes = n - (x + m); // size of the remainder of the file
byte[] data = new byte[nBytes]; // buffer to hold the remainder of the file
fs.Seek(x + m, SeekOrigin.Begin); // move to the end of the string
fs.Read(data, 0, nBytes);  // read the remainder
fs.Seek(-m, SeekOrigin.Current); // move to the begining of the string
fs.Write(data, 0, nBytes); // overwrite with the remainder of the file
fs.SetLength(n - m); //truncate the file
 
How u declare the fs instance?
As far as I know, both write and read instance do not have method seek.

All I want to do is actually delete the content of the file. Sorry for being so week in English, but I will improve soon.

Thanks
 
you can also read the file using the System.IO.StreamReader class , then use the replace function of the string class to remove your string , eg:
C#:
		[COLOR=Blue]string[/COLOR] path = [COLOR=Purple]@"C:\a text file.txt"[/COLOR]; [COLOR=Green]// path to the text file.[/COLOR]
		System.IO.StreamReader sread = [COLOR=Blue]new[/COLOR] System.IO.StreamReader( path , System.Text.Encoding.Default);
		[COLOR=Blue]string[/COLOR] temp = sread.ReadToEnd(); [COLOR=Green]// read the file[/COLOR]

		temp = temp.Replace("some" , ""); [COLOR=Green]// remove the string ( in this case the word  some  )[/COLOR]		
                sread.Close();
		[COLOR=Green]/* make sure we close the StreamReader , so we can write the file back with the string removed.
		 now we can write the file back ( minus the string  some  ) */[/COLOR]
		System.IO.StreamWriter swrite = [COLOR=Blue]new[/COLOR] System.IO.StreamWriter( path , [COLOR=Blue]false[/COLOR] , System.Text.Encoding.Default);
		swrite.Write(temp);
		swrite.Close();
 
Oops!...IOException occur

Hi, I try to study both the code snippet and samples frm MSDN...
After that, I try to do. But on the statement
[VB]
StreamWriter swrite = new StreamWriter(FILE, false, System.Text.Encoding.Default);
swrite.Write(temp);
swrite.Close();
[/VB]

it cause IOException error... How I deal with it?
Is it solve using try...catch...finally?
If it is, what should I put int try? catch? and finally?
 
Whats the IOException error? I havent done any I/O handling in C#, but is it similar to Java, requiring a throws clause to be attached to the method signature?
 
Thank for the reply Winston, I know what happen.
In VS.NET we actually can see the value of the variable flow.

Refer to dynamic sysop reply to me, the statement
[VB]
StreamReader sread = new StreamReader(FILE);
string temp = sread.ReadToEnd();
[/VB]
variable temp is null.
thats why the program do not run properly.

Can this problem be treated?
By right the temp variable should hold value which extract from the file in FILE value.
 
Ok I got it

the temp is not NULL now.

but statement
[VB]
temp.Replace("some","");
[/VB]

does not replace the string.
suppose the file look like this
.......
abc
some thing
sometimes
.......

these some is not change at all.
I tried it myself
 
azae said:
the temp is not NULL now.

but statement
[VB]
temp.Replace("some","");
[/VB]

does not replace the string.
suppose the file look like this
.......
abc
some thing
sometimes
.......

these some is not change at all.
I tried it myself

The code works fine, just small overlooked minor problem i suppose.

[VB]

public void removeStringFromFile(string stringToRemove, string filePath)
{

System.IO.StreamReader reader = new System.IO.StreamReader(filePath);
string streamRead = reader.ReadToEnd();
string removedString;
reader.Close();
removedString = streamRead.Replace(stringToRemove,"");
System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath);
writer.Write(removedString);
writer.Close();

}

[/VB]

That will definately work, the reason why it wasnt working was because the Replace function was called, and it returns a new string that is the modified version, you should notice that the string class is immutable, meaning when you call any of its function it tends not to modify the actual data instead use it and modify it and return it in a new string.
 
Back
Top