2

I do not know why in the last csv line the last 4 columns are cut off

Maybe that helps if I briefly describe the process.

Initially, the app reads in a csv, which is output in a ListView. The individual ListItems can be selected. For each item, a time with date can be stopped, these should then be written to the csv. This works in which I overwrite the whole csv.

After restarting the app appears only "Unhandled exception" because the overwritten csv no longer matches the original. All records are ok except the last and there are the last 4 values:

_Dum "+", "+" _Vo "+", "+" _Bi "+", "+" _Da "

builder.AppendLine( "_Dum" + "," + "_Vo" + "," + "_Bi" + "," + "_Da" + "\r");

//Add Data

foreach (var data in varModule.List)
{
builder.AppendLine( data._Dum + ",");

if (data._Stu == "B" || data._Stu == "E")
{
builder.AppendLine(data._Vo + "," + data._Bi + "," + data._Dau);
};

//Line Break

 builder.AppendLine("\r\n\n");
 }
using (StreamWriter sw = new StreamWriter(csvFilePath))

{
 sw.WriteLine(builder);
 sw.Flush();
}

Note: If I dont wirte \r\n\n and double || the last csv row would be cut of earlier.

C.J.Smith
  • 13
  • 3
  • 1
    Something seems very wrong with your *carriage return, newline* approach. Can you describe why you add the `"\r"` inside the `AppendLine` in line 1 and why you write `AppendLine("\r\n\n")`? If it's just a workaround that seems to be fixing something for you but that's not following a documented problem/solution, then we would end up yakk shaving if we don't fix this thing first. – grek40 Nov 20 '17 at 08:33
  • To extend to what @grek40 said, if you want to write newline characers yourself, use `builder.Append()` instead of `builder.AppendLine()`. – C.Evenhuis Nov 20 '17 at 08:52

1 Answers1

0

It doesn't seem that your program writes the data you need. You probably expect the following output

_Dum,_Vo,_Bi,_Da<NewLine>

<ValueOf_Dum>,<ValueOf_Vo>,<ValueOf_Bi>,<ValueOf_Da><NewLine>

Note that I specifically added <NewLine> to the output. <NewLine> stands for the newline character(s), which depending on your OS is either "\n" (on Unix systems) or "\r\n" (on Windows systems). In .NET there is also Environment.NewLine, that returns the platform-specific new line characters. Note that there is no CSV "standard", so it's your choice wich new line characters you want to use. It seems that you are not clear on that, so you first have to make that decision.

The next thing you have to consider is the difference between StringBuilder.Append and StringBuilder.AppendLine. Append simply adds the specified text at the end of the string, while AppendLine adds the text, followed by the characters returned by Environment.NewLine.

In your code you consistently use AppendLine which can not be what you want, since you obviously want to construct lines and add them as a whole. The way you do it, you consistently add new line characters between some of your values, which in CSV means the start of a new record.

You not only inadvertently add new line characters to your file, you are also not consistent in the new line characters you manually add. In the title row you use "\r" (which is not OK in Unix and in Windows). Later you use "\r\n\n" (even followed by the new line added by AppendLine), which does not work well in Unix (because of the "\r" at the beginning) or in Windows (because of the "\n" without a preceding "\r" at the end).

Your next problem is this part of your code:

if (data._Stu == "B" || data._Stu == "E")
{
    builder.AppendLine(data._Vo + "," + data._Bi + "," + data._Dau);
};

You are adding the values for _Vo, _Bi and _Dau when _Stu is "B" or "E". And what do you do when _Stu has any other value? Nothing! That means you generate an incomplete record there, which is the most likely reason for the problem you describe. You need to make up your mind what should happen in the else case and incorporate it into your program.

In general you seem to not be fully clear on the intended flow of your program, the CSV format and what the instructions you are using are doing in your code. To solve your problem you need to do the following steps:

  1. Get a clear understanding of the desired output and how a program must look that generates that output.
  2. Understand the CSV format and the significance of a line break in it.
  3. Decide about the new line format you want to use ("\r\n", "\n" or Environment.NewLine) and use it consistently throughout your program.
  4. Use Append where you want to add just the text and AppendLine when you want the added text to be followed by Environment.NewLine.
  5. Clear up what should happen when Stu is not "B" or "E" and add this case to your program.
  6. Step through your program in a debugger to confirm that it is doing exactly what you inted it to do.
Community
  • 1
  • 1
Sefe
  • 13,731
  • 5
  • 42
  • 55