7
Answers

filling a csv file

Hello, I have a code that reads some data and fill it into csv file
what i need to do is to repeat each line but with increasing the sequence column by one.
The out put of the csv file is as follows:
PROJECT;SEQUENCE;STEP;XPOS;YPOS;ZPOS;WRIST;WRISTROT;SPEED;PAUSE
1084;1;1;-12;-18;0;71;-51;30;400
1084;2;1;13;-18;0;71;-51;30;400
1084;3;1;0;0;0;71;-51;30;0
1084;4;1;0;0;0;71;-51;30;0
1084;5;1;18,33;5,46;20;71;-51;30;400
1084;6;1;18,33;5,46;20;71;-51;30;2000
1084;7;1;18,33;5,46;20;71;-51;30;400
1084;8;1;36,57;6,27;20;71;-51;30;400
1084;9;1;36,57;6,27;20;71;-51;30;400
1084;10;1;36,57;6,27;20;71;-51;30;400
1084;11;1;36,57;6,27;20;71;-51;30;0
1084;12;1;-12;-18;0;71;-51;30;400
The code is as follows: 
Note that XX.Count = 9, I want to repeat each line after the original line starting from buffer[2]. So the First two lines (buffer[0],buffer[1]) and the last line buffer[buffer.Length-1]should not be repeated.. I just want to repeat each line but with increasing the sequence.
so the output of the sequence will be 1,2,3,4,5,6,7,8.... till buffer.Length-1
static void generatecsv(int MobileID)
{
string ProgramPfad = (@"C:\Robotron\MobileRobotDaten\" + MobileID + ".csv");
List<double> XX, YY, ZZ;
List<int> VV;
Menu_Tree(MobileID, out XX, out YY, out ZZ, out VV);
string[] buffer = new string[XX.Count + 3];
using (System.IO.StreamWriter file = new System.IO.StreamWriter(ProgramPfad))
{
double x_robot = -12.0; 
double y_robot = -18.0; 
double z_robot = 0.0;

double x_distance = 25.0;
double y_distance = 0.0;


file.WriteLine("PROJECT;SEQUENCE;STEP;XPOS;YPOS;ZPOS;WRIST;WRISTROT;SPEED;PAUSE");


buffer[0] = (MobileID + ";" + 1 + ";" + 1 + ";" + x_robot + ";" + y_robot + ";" + z_robot + ";" + 71 + ";" + -51 + ";" + 30 + ";" + 400);
buffer[1] = (MobileID + ";" + 2 + ";" + 1 + ";" + (x_robot + x_distance) + ";" + (y_robot + y_distance) + ";" + z_robot + ";" + 71 + ";" + -51 + ";" + 30 + ";" + 400);

file.WriteLine(buffer[0]);
file.WriteLine(buffer[1]);

for (int i = 2; i < buffer.Length - 1; i++)
{
if (VV[i - 2] == -1)
{
buffer[i] = (MobileID + ";" + (i + 1) + ";" + 1 + ";" + XX[i - 2] + ";" + YY[i - 2] + ";" + ZZ[i - 2] + ";" + 71 + ";" + -51 + ";" + 30 + ";" + 400);
}
else
{
buffer[i] = (MobileID + ";" + (i + 1) + ";" + 1 + ";" + XX[i - 2] + ";" + YY[i - 2] + ";" + ZZ[i - 2] + ";" + 71 + ";" + -51 + ";" + 30 + ";" + (VV[i - 2] * 1000));
}

file.WriteLine(buffer[i]);
}


buffer[buffer.Length - 1] = buffer[0] = (MobileID + ";" + (buffer.Length) + ";" + 1 + ";" + x_robot + ";" + y_robot + ";" + z_robot + ";" + 71 + ";" + -51 + ";" + 30 + ";" + 400);
file.WriteLine(buffer[buffer.Length - 1]);
}


Answers (7)