Creating New text file having \t replaced with \n

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 57
Reputation: emarshah is an unknown quantity at this point 
Solved Threads: 5
emarshah emarshah is offline Offline
Junior Poster in Training

Creating New text file having \t replaced with \n

 
0
  #1
Jul 5th, 2009
Hello all,

Thanks in advance for any type of help, i want to create a text file from already created textfile, which will have all the "\t" characters replaced with "\n" e.g

abc.txt

ammar hassan
shah naqvi

new.txt

ammar
hassan
shah
naqvi

here is the code i m using, but it's not showing my required output

  1. String line = "";
  2.  
  3.  
  4.  
  5. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  6. {
  7. textBox1.Text = openFileDialog1.FileName;
  8.  
  9.  
  10.  
  11. ArrayList arr = new ArrayList();
  12.  
  13. try
  14. {
  15. // Create an instance of StreamReader to read from a file.
  16. // The using statement also closes the StreamReader.
  17. using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
  18. {
  19. // Read and display lines from the file until the end of
  20. // the file is reached.
  21. while ((line = sr.ReadLine()) != null)
  22. {
  23.  
  24. foreach (string s in line.Split(new char []{'\t'}))
  25. {
  26. string ss = s;
  27. ss += "\n";
  28. arr.Add(ss);
  29.  
  30. } //Console.WriteLine(line);
  31. }
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. // Let the user know what went wrong.
  37. MessageBox.Show(ex.Message);
  38. }
  39.  
  40.  
  41. // MessageBox.Show(line);
  42.  
  43.  
  44. string str = "";
  45.  
  46. for (int i = 0; i < arr.Count; i++)
  47. {
  48. //using (sw)
  49. //{
  50. str += arr[i].ToString();
  51. //}
  52. }
  53. MessageBox.Show(str);
  54.  
  55. using (StreamWriter sw = new StreamWriter("TestFile.txt"))
  56. { sw.WriteLine("ghv");
  57. sw.Write("g");
  58. sw.WriteLine("ghv");
  59. }
  60. }
Syed Ammar Hassan
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Creating New text file having \t replaced with \n

 
0
  #2
Jul 5th, 2009
Your code seems pretty OK.
In the example you give ammar hassan, could it be that there is a space char instead of a tab char between ammar and hassan?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 57
Reputation: emarshah is an unknown quantity at this point 
Solved Threads: 5
emarshah emarshah is offline Offline
Junior Poster in Training

Re: Creating New text file having \t replaced with \n

 
0
  #3
Jul 5th, 2009
To ddanbe:
No, there are no spaces, only tabs between two words....
Syed Ammar Hassan
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,562
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 454
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Creating New text file having \t replaced with \n

 
0
  #4
Jul 5th, 2009
Why not string replace method?
  1. string b = a.Replace('\t','\n');
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Creating New text file having \t replaced with \n

 
0
  #5
Jul 5th, 2009
Used this as a test and it worked with me.
  1. namespace ConsoleApplication1
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. string line = "ammar\thassan\nshah\tnaqvi";
  8. ArrayList arr = new ArrayList();
  9. foreach (string s in line.Split(new char[] { '\t' }))
  10. {
  11. arr.Add(s + "\n");
  12. }
  13. Console.WriteLine(line);
  14. string str = "";
  15. for (int i = 0; i < arr.Count; i++)
  16. {
  17. str += arr[i].ToString();
  18. }
  19. Console.WriteLine(str);
  20. Console.ReadKey();
  21. }
  22. }
  23. }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 57
Reputation: emarshah is an unknown quantity at this point 
Solved Threads: 5
emarshah emarshah is offline Offline
Junior Poster in Training

Re: Creating New text file having \t replaced with \n

 
0
  #6
Jul 5th, 2009
to Adatapost:

sir, i have checked with this method too, but didn't find my required string in file. Actually the string when retrieved from file, we are able to change that string using replace etc... and store in a new string. But when we want to store this new string in a new text file, the string is saved without any "\n" characters..... :-(
Syed Ammar Hassan
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 57
Reputation: emarshah is an unknown quantity at this point 
Solved Threads: 5
emarshah emarshah is offline Offline
Junior Poster in Training

Re: Creating New text file having \t replaced with \n

 
0
  #7
Jul 5th, 2009
to ddanbe:
I have checked this method too, this will create string with "\t" char replaced with "\n" but we when we store that string in file, all the "\n" dont appear.....
:-(
Syed Ammar Hassan
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,562
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 454
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Creating New text file having \t replaced with \n

 
0
  #8
Jul 5th, 2009
emarshah,

I am not agree with what you said :
But when we want to store this new string in a new text file, the string is saved without any "\n" characters.....
  1. string a = System.IO.File.ReadAllText(@"c:\csnet\a1.txt");
  2. string b = a.Replace('\t','\n');
  3. System.IO.File.WriteAllText(@"c:\csnet\a2.txt",b);
Last edited by adatapost; Jul 5th, 2009 at 6:51 am. Reason: Typo
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Creating New text file having \t replaced with \n

 
0
  #9
Jul 5th, 2009
Try replacing "\t" with "\r\n"
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 57
Reputation: emarshah is an unknown quantity at this point 
Solved Threads: 5
emarshah emarshah is offline Offline
Junior Poster in Training

Re: Creating New text file having \t replaced with \n

 
0
  #10
Jul 5th, 2009
Sir,
Actually, when it the string is written in text file, there are no '\t' replaced with '\n', i m showing you my input and output file,

input file

ammar hassan
shah naqvi
qasim nazeer


output file

ammarhassan
shahnaqvi
qasimnazeer

the problem is still there and my required output is;

Required Output file

ammar
hassan
shah
naqvi
qasim
nazeer



Originally Posted by adatapost View Post
emarshah,

I am not agree with what you said :


  1. string a = System.IO.File.ReadAllText(@"c:\csnet\a1.txt");
  2. string b = a.Replace('\t','\n');
  3. System.IO.File.WriteAllText(@"c:\csnet\a2.txt",b);
Syed Ammar Hassan
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC