StreamWriter add an extra \r in the end of the line

前端 未结 4 1915
我在风中等你
我在风中等你 2021-01-18 08:32

I created a class with the responsibility to generate a text file where each line represents the information of an object of \'MyDataClass\' class. Below is a simplification

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 09:30

    I just now had a similar problem where the code below would randomly insert blank lines in the output file (outFile)

    using (StreamWriter outFile = new StreamWriter(outFilePath, true)) {
         foreach (string line in File.ReadLines(logPath)) {
                string concatLine = parse(line, out bool shouldWrite);
                if (shouldWrite) {
                  outFile.WriteLine(concatLine);       
                }     
          }   
    }
    

    Using Antar's idea I changed my parse function so that it returned a line with Environment.NewLine appended, ie

    return myStringBuilder.Append(Environment.NewLine).ToString();
    

    and then in the foreach loop above, changed the

    outFile.WriteLine(concatLine);
    

    to

    outFile.Write(concatLine);
    

    and now it writes the file without a bunch of random new lines inserted. However, I still have absolutely no idea why I should have to do this.

提交回复
热议问题