| | |
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:
Solved Threads: 5
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
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
c# Syntax (Toggle Plain Text)
String line = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; ArrayList arr = new ArrayList(); try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader(openFileDialog1.FileName)) { // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { foreach (string s in line.Split(new char []{'\t'})) { string ss = s; ss += "\n"; arr.Add(ss); } //Console.WriteLine(line); } } } catch (Exception ex) { // Let the user know what went wrong. MessageBox.Show(ex.Message); } // MessageBox.Show(line); string str = ""; for (int i = 0; i < arr.Count; i++) { //using (sw) //{ str += arr[i].ToString(); //} } MessageBox.Show(str); using (StreamWriter sw = new StreamWriter("TestFile.txt")) { sw.WriteLine("ghv"); sw.Write("g"); sw.WriteLine("ghv"); } }
Syed Ammar Hassan
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
•
•
Join Date: Jan 2008
Posts: 57
Reputation:
Solved Threads: 5
To ddanbe:
No, there are no spaces, only tabs between two words....
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---------------------------
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
Why not string replace method?
C# Syntax (Toggle Plain Text)
string b = a.Replace('\t','\n');
Failure is not fatal, but failure to change might be. - John Wooden
Used this as a test and it worked with me.
c# Syntax (Toggle Plain Text)
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string line = "ammar\thassan\nshah\tnaqvi"; ArrayList arr = new ArrayList(); foreach (string s in line.Split(new char[] { '\t' })) { arr.Add(s + "\n"); } Console.WriteLine(line); string str = ""; for (int i = 0; i < arr.Count; i++) { str += arr[i].ToString(); } Console.WriteLine(str); Console.ReadKey(); } } }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Jan 2008
Posts: 57
Reputation:
Solved Threads: 5
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..... :-(
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---------------------------
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
•
•
Join Date: Jan 2008
Posts: 57
Reputation:
Solved Threads: 5
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.....
:-(
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---------------------------
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
emarshah,
I am not agree with what you said :
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.....
C# Syntax (Toggle Plain Text)
string a = System.IO.File.ReadAllText(@"c:\csnet\a1.txt"); string b = a.Replace('\t','\n'); 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
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Jan 2008
Posts: 57
Reputation:
Solved Threads: 5
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
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
•
•
•
•
emarshah,
I am not agree with what you said :
C# Syntax (Toggle Plain Text)
string a = System.IO.File.ReadAllText(@"c:\csnet\a1.txt"); string b = a.Replace('\t','\n'); 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---------------------------
" A man is known by his level of Determinism "
--------------------------------------------------------
"Some cause happiness where ever they go, others whenever They go"
--------------------------XIII---------------------------
![]() |
Similar Threads
- Create and send text file (PHP)
- How to read text file and display it in a messagebox (ASP.NET)
- text file managment with fstream (C++)
- Creating a Login Text file (VB.NET)
- Help with creating text file with "" (C#)
- how to read a text file backwards? (C)
- Search string in a text file (C)
- Trying to creating an array from a text file (C++)
- write to text file using fprintf() (C)
Other Threads in the C# Forum
- Previous Thread: c# client, java server
- Next Thread: Practice Exercises in C#
| Thread Tools | Search this Thread |
.net access algorithm animation array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom customactiondata database datagrid datagridview dataset datastructure date/time datetime datetimepicker degrees development dll draganddrop drawing encryption enum event excel file filename files form format forms function gdi+ gis gtk hash image index input install java label list listbox mandelbrot math mouseclick mysql operator outlook2003 path photoshop picturebox pixelinversion pixelminversion post programming radians regex remoting richtextbox server sleep snooze socket sql statistics stream string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser webcam wia windows winforms wpf xml






