replacing new lines and tab spacing with a single space
-
I am passing a text file using streamreader and filestream.......My moto is to replace the new lines and tab spacing with a single space....But instead the resulting text file is empty once my project is execute......... plz help !!! using System.IO; . . . . . string path="C://test.txt"; Streamreader re=File.Openext(path); StreamWriter rw=new StreamWriter(path); while((input=re.ReadAllLines())!=null) { if(input=="\n") rw.Write(" "); if(input=="\t") rw.Write(" "); } .......whr am i goin wrong ???
-
I am passing a text file using streamreader and filestream.......My moto is to replace the new lines and tab spacing with a single space....But instead the resulting text file is empty once my project is execute......... plz help !!! using System.IO; . . . . . string path="C://test.txt"; Streamreader re=File.Openext(path); StreamWriter rw=new StreamWriter(path); while((input=re.ReadAllLines())!=null) { if(input=="\n") rw.Write(" "); if(input=="\t") rw.Write(" "); } .......whr am i goin wrong ???
One suggestion would be to check out the members of the StreamReader and StreamWriter classes, along with sample code. Try using Read() or ReadLine() instead of the ReadAllLines() in your while loop condition. http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^] http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^] Also, you might want to consider copying the data as you read it to a new text file and overwriting the old one when you are finished. And don't forget to close the filestreams when complete.
modified on Monday, April 20, 2009 10:49 AM
-
I am passing a text file using streamreader and filestream.......My moto is to replace the new lines and tab spacing with a single space....But instead the resulting text file is empty once my project is execute......... plz help !!! using System.IO; . . . . . string path="C://test.txt"; Streamreader re=File.Openext(path); StreamWriter rw=new StreamWriter(path); while((input=re.ReadAllLines())!=null) { if(input=="\n") rw.Write(" "); if(input=="\t") rw.Write(" "); } .......whr am i goin wrong ???
1 You should output to a new file not the one you are reading in. For example use:
string path="C://test.txt";
Streamreader re=File.Openext(path);
StreamWriter rw=new StreamWriter(@"C:\test_out.txt"));2 Your loop is wrong. Look at what ReadAllLines() does in help. You probably want something like:
string input;
StringBuilder sb = new StringBuilder("");
while((input=re.ReadLine())!=null)
{
input = input.Replace('\t', ' '); //replace tabs
sb.Append(input + " ");
}
rw.Write(sb.ToString());The StringBuilder is used because ReadLine() drops the new lines (so you don't need to replace them) and you need to add the space after the line. 3 Have a look at File.ReadAllText() and Replace() for strings. It might be simpler to use these.
Regards David R
-
1 You should output to a new file not the one you are reading in. For example use:
string path="C://test.txt";
Streamreader re=File.Openext(path);
StreamWriter rw=new StreamWriter(@"C:\test_out.txt"));2 Your loop is wrong. Look at what ReadAllLines() does in help. You probably want something like:
string input;
StringBuilder sb = new StringBuilder("");
while((input=re.ReadLine())!=null)
{
input = input.Replace('\t', ' '); //replace tabs
sb.Append(input + " ");
}
rw.Write(sb.ToString());The StringBuilder is used because ReadLine() drops the new lines (so you don't need to replace them) and you need to add the space after the line. 3 Have a look at File.ReadAllText() and Replace() for strings. It might be simpler to use these.
Regards David R
my input in the text file is somethin like : hi this is mist I need the output as : hi this is mist ...... i tried using ur tips for removing the new lines and replace them with a single space but could'nt do....and how can i get the output in d same text file only ?? need help..... !!..plz
-
my input in the text file is somethin like : hi this is mist I need the output as : hi this is mist ...... i tried using ur tips for removing the new lines and replace them with a single space but could'nt do....and how can i get the output in d same text file only ?? need help..... !!..plz
StringBuilder sb = new StringBuilder(); string line; using (StreamReader sr = new StreamReader(fileName)) { while ((line = sr.ReadLine()) != null) { sb.Append(line + " "); } } line = sb.ToString(); // Do what you need with the output data.
Now, read a manual, and do your own homework next time!
-
my input in the text file is somethin like : hi this is mist I need the output as : hi this is mist ...... i tried using ur tips for removing the new lines and replace them with a single space but could'nt do....and how can i get the output in d same text file only ?? need help..... !!..plz
-
I am passing a text file using streamreader and filestream.......My moto is to replace the new lines and tab spacing with a single space....But instead the resulting text file is empty once my project is execute......... plz help !!! using System.IO; . . . . . string path="C://test.txt"; Streamreader re=File.Openext(path); StreamWriter rw=new StreamWriter(path); while((input=re.ReadAllLines())!=null) { if(input=="\n") rw.Write(" "); if(input=="\t") rw.Write(" "); } .......whr am i goin wrong ???
Do you want multiple whitespace characters changed to one SPACE? Or each whitespace character changed to a SPACE?