954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please anybody to help to read string value in text file using C#

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.

Rocksoft
Newbie Poster
12 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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

RwCC
Junior Poster
177 posts since Jan 2006
Reputation Points: 70
Solved Threads: 4
 
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.

Rocksoft
Newbie Poster
12 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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();
RwCC
Junior Poster
177 posts since Jan 2006
Reputation Points: 70
Solved Threads: 4
 

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.

Rocksoft
Newbie Poster
12 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You