Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string[] file1 =( File.ReadAllLines(textBox1.Text));
        //string[] file2;

        int size = file1.Length;





using (FileStream fs = File.Open( "memory.txt",FileMode.OpenOrCreate))
            {
                using (TextReader r = new StreamReader(fs))
                {
                    for (i = 0; i < size; i++)
                    {
                        array[i] =Convert.ToInt32 (r.ReadLine());
                        listBox1.Items.Add(array[i]);




                    }


                }


            }
            string[] file2;
            using (FileStream fs1 = File.Create("dest.txt"))
            {
                using (TextWriter w = new StreamWriter(fs1))
                {
                    for (i = 0; i < size; i++)
                    {///here i want to add the contents of array1...to dest.txt...
                        
                    }
                }
            }




        }
    }
}

i am unable to perform...stream writer function ...above is the code...

textbox1.text will take a txt.file i.e (memory.txt)...

[Edit]Code block added[/Edit]
Posted
Updated 24-Feb-13 4:15am
v4

Hi,
you are doing a lot of things wrong in your code above.

1) Reading a text from from a text box into file without checking for empty string.
2) Reading the text box text into string array;
3) Checking the length of the array as size: this will be 1 in this case as only one text box has been read.
4) Opening a file with File "OpenCreate" this means if the file doesn't exists, then create one; then reading the file with stream reader, this is unnecessary.
5) For loop with "Size", if the text box was empty, then the size will be 0, if not then it will be 1; however;
6) Reading a line from the file that was opened with "OpenCreate", if the file was just created, it will return null; and this is converted to int32, this will throw exception that has not been caught.
7) Again using FileStream, FileCreate and StreamWriter; totally unnecessary.

Have you consider the code below:

public void WriteToFile()
        {
            string fileName_1 = "memory.txt";
            string fileName_2 = "dest.txt";
            List<string> readIn = new List<string>();
            FileInfo fi = new FileInfo(fileName_1);

            // Check that the file exists first
            if (!fi.Exists)
            {
                MessageBox.Show("Memory file doesn't exists!");
                return;
            }
            else
            {
                using (StreamReader sr = new StreamReader(fileName_1, System.Text.Encoding.Default))
                {
                    string str = "";
                    while ((str =sr.ReadLine()) != null)
                    {
                        readIn.Add(str);
                    }
                }
            }

            if (readIn.Count > 0)
            {
                StringBuilder sb = new StringBuilder();

                foreach (string str in readIn)
                {
                    sb.Append(str + "\r"); // add carriage return if necessary.
                }

                using (TextWriter textWriter = new StreamWriter(fileName_2, true)) // append to file if necessary
                {
                    textWriter.Write(sb.ToString());
                }
            }
        }


Regards
Jegan
 
Share this answer
 
v2
Comments
luckycode 24-Feb-13 13:44pm    
thanks a lot...many doubts abt streams have been clarified...thanks for the detailed program..
i am new to dotnet...so i am making many silly mistakes..
///here i want to add the contents of array1...to dest.txt...
Try:
C#
w.WriteLine(array[i]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900