-2

I have searched numerous web pages but I can't seem to find anywhere which shows how to stop a blank line appearing at the end of a file when using StreamWriter.

The code I've written below is the only way I can get this to work for me.. and although this works perfectly fine for the utility I am creating, I would like to know if there is a better/more efficient way to do this?

        int count = 0;
        int lineCount = newFile.Count;

        using (System.IO.StreamWriter extract = new System.IO.StreamWriter(outputFile, true))
        {
            foreach (var line in newFile)
            {
                count++;

                if (count != lineCount)
                {
                    extract.Write(line + Environment.NewLine);
                }
                else
                {
                    extract.Write(line);
                }
            }
        }

Any thoughts people?

  • And if you use WriteLine instead of Write(x + Environment.NewLine)? – oerkelens Jul 27 '20 at 17:28
  • 3
    Or even shorter `File.WriteAllLines(outputFile, newFile);`. Saves 15 lines of code :) – oerkelens Jul 27 '20 at 17:30
  • 3
    What do you mean by “a blank line appearing at the end of a file”? Do you mean after the last character of data there is a newline? That is usually what you want; by convention text files end with newlines, rows are not delimited with newlines. Consider catenating or streaming files; if they didn't end with newlines the last line of one file would be merged with the first line of the next file. Any utility you are creating should be able to deal with text files ending in newlines. – Dour High Arch Jul 27 '20 at 17:32
  • 1
    @oerkelens That will still include a newline on the end of the last line. – juharr Jul 27 '20 at 17:34
  • Use `for` loop instead of `foreach`. – Alexander Petrov Jul 27 '20 at 17:43
  • 1
    Show an example of the file contents and the code you are using that doesn't work. You've showed your workaround, not the original issue. – Charleh Jul 27 '20 at 17:50
  • @Charleh - I was just using: `using (System.IO.StreamWriter file = new System.IO.StreamWriter(outputFile, true)) { file.WriteLine(line); }` The file was writing, but when I scrolled to the bottom there was an empty line, like a carriage return line. – AGB_DT_54302290 Jul 27 '20 at 18:20
  • A carriage return is not an “empty line” and [text files should end with a newline](https://stackoverflow.com/questions/729692/). An empty line is two newlines in sequence, and what you have posted will not remove that. – Dour High Arch Jul 29 '20 at 20:18

2 Answers2

0

Since you are accessing the Count property, it means that your newFile source implements the IList interface. This means that you can access it by its index.

Use for loop instead of foreach:

for (int i = 0; i < newFile.Count - 1; i++)
{
    extract.WriteLine(newFile[i]);
}
extract.Write(newFile[newFile.Count - 1]);
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
-2

Check this...

var values = Enumerable.Range(1,10);

var output = string.Format(string.Join(",{0}", values), Environment.NewLine);

File.WriteAllText(path, output);
Vivek Bani
  • 3,703
  • 1
  • 9
  • 18
  • 1
    It creates large string in memory. – Alexander Petrov Jul 27 '20 at 17:43
  • @Alexander Petrov Your code will break if the array is empty `if (newFile.Any()) { for (int i = 0; i < newFile.Count() - 1; i++) { extract.WriteLine(newFile[i]); } extract.Write(newFile[newFile.Count() - 1]); }` – Vivek Bani Jul 27 '20 at 18:42