Hi,


I have used C# in vs 2003, in my application i have to read text file, pick data from text file and insert into database, but the text file have some garbage value (garbage value "Ú" ) , so i should replcace this garbage value to single space, i tried to use stream reader to read text file, but the problem is stream reader not at all reading this garbage value, its awqlays neglecting this garbage value, the following code i have used in my application


StreamReader reader = new StreamReader(@"d:\web_extract.txt");
string str = reader.ReadToEnd();
StringBuilder strbuild = new StringBuilder(str);
string gb = "Ú";
reader.Close();
strbuild.Replace(gb," ");
StreamWriter writer = new StreamWriter(@"d:\web_extract.txt");
writer.Write(strbuild.ToString());
writer.Close();

please anybody help me to solve this problem and take me out, Advanced thanks for any reply.

Thanks.

Ok I am rather confused about what you want, so the Ú is not meant to be there but is added when you read?

Ok I am rather confused about what you want, so the Ú is not meant to be there but is added when you read?

Hi,

Thanks for your immedite reply, I want to rplace this string value( Ú ) to single space in my text file using C#, but problem is stream reader not reading this string value, could you please give some some way to read the text file in C#.


Thanks.

Im still a little unsure of the problem (doing many things at once, not good!). Is it happening like this:

original file
Hello There

file contents from S/Reader
HelloÚThere

Is that how it is coming across? I used to get problems with Stream Reader This code (which is basically the same as yours) works fine without any strange additional characters:

System.IO.StreamReader StreamReader1 =
            new System.IO.StreamReader("myfile.txt");
            String data = StreamReader1.ReadToEnd();
            textBox.Text = data;
            StreamReader1.Close();

Im still a little unsure of the problem (doing many things at once, not good!). Is it happening like this:

original file
Hello There

file contents from S/Reader
HelloÚThere

Is that how it is coming across? I used to get problems with Stream Reader This code (which is basically the same as yours) works fine without any strange additional characters:

System.IO.StreamReader StreamReader1 =
            new System.IO.StreamReader("myfile.txt");
            String data = StreamReader1.ReadToEnd();
            textBox.Text = data;
            StreamReader1.Close();

Hi,

Thanks for reply, i have solved the problem, i have added the Encoding.Default to stream reader , now i'm able read the garbage value in my file,
StreamReader reader = new StreamReader(@"d:\web_extract.txt",Encoding.Default);

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.