-1

I'm creating a C# code that is required to read a text file line by line and then copy each line onto a new text file. I was able to figure out how to read line by line, but I'm having trouble copying line by line to the new text file I created.

This is what I am using to read my original text file line by line:

        int counter = 0;
        string line;

        // Read the file and display it line by line.
        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\AnswerFile.txt");

        while ((line = file.ReadLine()) != null)
        {
            System.Console.WriteLine(line);
            counter++;
        }

        file.Close();
        System.Console.WriteLine("There were {0} lines.", counter);
        // Suspend the screen.
        System.Console.ReadLine();

Any help is appreciated! Thank you

EDIT: I did not forget to write the code that copies the text onto another text file. That is the part I am having trouble with. I tried using streamwriter while specifying the directory of the file I want the text to go to, but something wasn't right. I want to create a code that literally reads line by line from one text file and copies line by line (as it reads from the initial file) to the new text file. I hope that clarifies my question.

EDIT2: Figured it out guys. Thank you for all the help! I had to call my company's security department to grant me access to write in the c drive.

1 Answers1

-1
        int counter = 0;
        string line;

        try
        {
            // Read the file and display it line by line.
            using (System.IO.StreamReader file = new System.IO.StreamReader(@"C:\\AnswerFile.txt"))
            {
                using (System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(@"C:\outputFile.txt"))
                {
                    while ((line = file.ReadLine()) != null)
                    {
                        System.Console.WriteLine(line);
                        fileWriter.WriteLine(line);
                        counter++;
                    }
                }
            }
            System.Console.WriteLine("There were {0} lines.", counter);
            // Suspend the screen.
            System.Console.ReadLine();
        }
        catch (System.IO.IOException ex)
        {
            // Handle the Error Properly Here
        }

Updated answer in response to comments about missing exception handling and closing of File handles in case of error.

Gareth Oates
  • 399
  • 4
  • 14
  • 1
    Who's going to close your files if you get an error? – musefan Aug 12 '14 at 15:28
  • Wrap it in a Try Catch Finally block and do the File.Close() in the finally block. Or do some error handling. Figured doing that was outwith the scope of the question. – Gareth Oates Aug 12 '14 at 15:30
  • 1
    @Garerth: I'm just making a point, personally I would prefer to make sure the OP knows not to let it slip as they are clearly still learning some basics. But I won't knock your answer for not including it, it's not a requirement to solve the question. Also, I would recommend a `using` block instead of try/catch – musefan Aug 12 '14 at 15:34
  • Good point well made, musefan. – Gareth Oates Aug 12 '14 at 15:36
  • I tried that earlier, but it still gives me an issue. It highlights : System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(@"C:\outputFile.txt") and tells me "unauthorizedaccessexception was unhandled. Access to path is denied". I don't know why it is denying it if I created the file myself and I made sure it is accessible by checking that on the file's properties. Any ideas on how to resolve this? – رحاب صديق Aug 12 '14 at 15:43
  • @رحابصديق: That doesn't mean the code is wrong, it means you don't have access to write in that location. Try a different location, where you know you can definitely create new files – musefan Aug 12 '14 at 15:47
  • Yes, the code is correct throughout. I just don't know how to get access to write in that location. Any tips? This is what I get when I run --> "access to path c:\\... is denied". I don't have access to write in that location. – رحاب صديق Aug 12 '14 at 15:59
  • @musefan I've modified my code block to be more correct. Could you remove the downvote? – Gareth Oates Jan 12 '16 at 09:39
  • @GarethOates: I cannot do that, because it is not my downvote. You shouldn't worry about it, it isn't important – musefan Jan 12 '16 at 14:27
  • @musefan true. Sorry for assuming it was you! :) – Gareth Oates Jan 13 '16 at 09:55