Question

I have a text file with about 5,000 lines and I want to copy its contents to another file, but only the first 38 characters of each line.

I currently have this code:

private void button1_Click(object sender, EventArgs e)
    {
        string line, line2;

        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
        while ((line = file.ReadLine()) != null)
        {
            line2 = line.Substring(0, 38);
            using (System.IO.StreamWriter files = new System.IO.StreamWriter(@"C:\test2.txt"))
            {
                files.WriteLine(line2);
            }
        }

        file.Close();
    }

It only copies the last line. :(

Was it helpful?

Solution

because you rewrite your new file in your loop. You should create your new string in the loop (use a stringBuilder for this will be more efficient), but write the new file out of the loop :

    string line;
    var sb = new StringBuilder();
    System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
    while ((line = file.ReadLine()) != null)
        sb.AppendLine(line.Substring(0, Math.Min(38, line.Length)));

    file.Close();
    using (System.IO.StreamWriter files = new System.IO.StreamWriter(@"C:\test2.txt"))
        {
            files.WriteLine(sb.ToString());
        }

or to do it shorter

var result = File.ReadAllLines(@"c:\test.txt")
                 .Select(m => m.Substring(0, Math.Min(38, m.Length)));
File.WriteAllLines(@"C:\test2.txt", result);

OTHER TIPS

You need to move the creation of 'file2' before the while loop. You should also create 'file' in a using:. You won't need to call close for either one then.

private void button1_Click(object sender, EventArgs e)
{
    string line;

    using (System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt"))
        using (System.IO.StreamWriter file2 = new System.IO.StreamWriter(@"C:\test2.txt"))
            while ((line = file.ReadLine()) != null)
            {
                string line2 = line.Substring(0, 38);
                file2.WriteLine(line2);
            }
}

Your file's .WriteLine overwrites the entire file every time you call it. Therefore, put the entire code in your using block, or add true to the StreamWriter's arguments to tell it to append to the file instead of overwriting.

Option 1:

private void button1_Click(object sender, EventArgs e)
{
    string line, line2;
    using (System.IO.StreamWriter files = new System.IO.StreamWriter(@"C:\test2.txt"))
        {
        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
        while ((line = file.ReadLine()) != null)
        {
            line2 = line.Substring(0, 38);

            files.WriteLine(line2);

        }

        file.Close();
    }
}

Option 2:

private void button1_Click(object sender, EventArgs e)
    {
        string line, line2;

        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
        while ((line = file.ReadLine()) != null)
        {
            line2 = line.Substring(0, 38);
            using (System.IO.StreamWriter files = new System.IO.StreamWriter(@"C:\test2.txt",true))
            {
                files.WriteLine(line2);
            }
        }

        file.Close();
    }

And finally, if you choose to use a StringBuilder, you can use System.IO.File.WriteAllText(@"C:\test2.txt", stringHere); instead of the entire using and StreamWriter block

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top