0

Hi,

I have used stream reader to read text file using Readline method, and i tried to append all string to string builder, but i'm not able to get all string value, i'm only getting last line of file, i have used the following coding

StreamReader reader = new StreamReader(@"d:\web_extract.txt",Encoding.Default);
StringBuilder strbuild = null;
StreamWriter writer = null;
while(reader.Peek() >= 0)
{
string str = null;
str = reader.ReadLine().ToString();
str = str.Replace("Ú"," ");
strbuild = new StringBuilder();
// str = strbuild.Replace("Ú"," ").ToString();
strbuild.Append(str);


}
string str1 = strbuild.ToString();
reader.Close();
writer = new StreamWriter(@"d:\web_extract1.txt",true);
writer.Write(str1);
writer.Close();

Please anybody help to solve this problem, advanced thanks for any reply.

Thanks.

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by _r0ckbaer
0

This is because you instantiate the stringbuilder object via

strbuild = new StringBuilder();

in the loop, you have to instantiate it outside when you first declare it

StreamReader reader = new StreamReader(@"d:\web_extract.txt",Encoding.Default);
StringBuilder strbuild = new StringBuilder(); <--here
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.