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

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");
            }
            }

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?

To ddanbe:
No, there are no spaces, only tabs between two words....

Why not string replace method?

string b = a.Replace('\t','\n');

Used this as a test and it worked with me.

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();
        }
    }
}

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..... :-(

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.....
:-(

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.....

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);

Try replacing "\t" with "\r\n"

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


emarshah,

I am not agree with what you said :

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);

O thats the solution .... thanks Ramy...

Try replacing "\t" with "\r\n"

Try replacing "\t" with "\r\n"

string a = System.IO.File.ReadAllText(@"c:\csnet\a1.txt");
            string b = a.Replace('\t','\r\n');
            System.IO.File.WriteAllText(@"c:\csnet\a2.txt",b);

You are welcome :)
Pretty simple just in file new line == \r\n not just \n
Please mark it as solved :)

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.