I have read references for writing data into csv file but my problem is that i wan to read data from another 2 files and select two specific elements (one from each file and then write them into the new file). Here is what i came to
The file is not generated and I want to know how can i get the y as the element in the other csv line
This is reading two csv files and generating a new one but i want to select the x from the first csv file and y from the second csv file.
static void LeftBottom(int MobileID)
{
string ProgramPfad = (@"C:\Robotron\Execution\"+MobileID+".csv");
int b = 2;
string linee;
List x= new List();
List y = new List();
int z = 25;
StreamReader reader = File.OpenText(@"C:\Robotron\Execution\TS_Right_Bottom.csv");
StreamReader rdr = File.OpenText(@"C:\Robotron\Execution\TS_Left_Top.csv");
while ((linee = reader.ReadLine()) != null)
{
string[] fields = linee.Split('\t');
if (fields.Length != 15) continue;
int Keyid = Convert.ToInt32(fields[0]);
int id = Convert.ToInt32(fields[1]);
if (id == MobileID)
{
x.Add(fields[5]);
y.Add(fields[6]);
break;
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter(ProgramPfad))
{
file.WriteLine("intMobileID;strTasten_Name;strTastenfunktion;strDruckrichtung;dblX_Wert;dblY_Wert;dblZ_Wert;dbldX_Wert;dbldY_Wert;dbldZ_Wert;intTastendruck;intDauer;intAnzahl;strBewegungstyp");
string[] buffer = new string[1];
buffer[b - 1] = (Keyid+";"+MobileID+";"+"TS_Left_Bottom"+";"+";"+"mitte"+";"+ x+";"+y +";"+z+";"+0+";"+0+";"+0+";"+12000+";"+100+";"+1+";"+"XY-Z");
}
}
}
{
string ProgramPfad = (@"C:\Robotron\Execution\"+MobileID+".csv");
int b = 2;
string linee;
List
List
int z = 25;
StreamReader reader = File.OpenText(@"C:\Robotron\Execution\TS_Right_Bottom.csv");
StreamReader rdr = File.OpenText(@"C:\Robotron\Execution\TS_Left_Top.csv");
while ((linee = reader.ReadLine()) != null)
{
string[] fields = linee.Split('\t');
if (fields.Length != 15) continue;
int Keyid = Convert.ToInt32(fields[0]);
int id = Convert.ToInt32(fields[1]);
if (id == MobileID)
{
x.Add(fields[5]);
y.Add(fields[6]);
break;
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter(ProgramPfad))
{
file.WriteLine("intMobileID;strTasten_Name;strTastenfunktion;strDruckrichtung;dblX_Wert;dblY_Wert;dblZ_Wert;dbldX_Wert;dbldY_Wert;dbldZ_Wert;intTastendruck;intDauer;intAnzahl;strBewegungstyp");
string[] buffer = new string[1];
buffer[b - 1] = (Keyid+";"+MobileID+";"+"TS_Left_Bottom"+";"+";"+"mitte"+";"+ x+";"+y +";"+z+";"+0+";"+0+";"+0+";"+12000+";"+100+";"+1+";"+"XY-Z");
}
}
}
VulpesPosted Oct 7, 2014, 11:04 AM
You'll therefore need to use a loop to display them:
Hesham HassaneinPosted Oct 7, 2014, 10:56 AM
Hesham HassaneinPosted Oct 7, 2014, 10:55 AM
static void GetLengthWidth(int MobileID)
{
// List
StreamReader readerr = File.OpenText(@"C:\Robotron\Execution\TS_Left_Top.csv");
string line3;
List
while ((line3 = readerr.ReadLine()) != null)
{
string[] fields = line3.Split(';');
if (fields.Length != 15) continue;
if (fields[1] == "intMobileID") continue;
int id = Convert.ToInt32(fields[1]);
if (id == MobileID)
{
yList.Add(fields[6]);
}
}
StreamReader rdrr = File.OpenText(@"C:\Robotron\Execution\TS_Right_Bottom.csv");
string line4;
List
while ((line4 = rdrr.ReadLine()) != null)
{
string[] fields = line4.Split(';');
if (fields.Length != 15) continue;
if (fields[1] == "intMobileID") continue;
int id = Convert.ToInt32(fields[1]);
if (id == MobileID)
{
xList.Add(fields[5]);
}
}
List
List
// List
List
List
Console.WriteLine("The length and width of the display of the current Mobile are" + Length + ";"+ Width);
Console.ReadKey();
readerr.Close();
rdrr.Close();
}
when i run the code it displays -------- ength and width of the display of the current Mobile are - and then : System.Collection.Generic. List 1 and thats it
VulpesPosted Oct 7, 2014, 10:47 AM
Hesham HassaneinPosted Oct 7, 2014, 10:34 AM
Hesham HassaneinPosted Oct 7, 2014, 10:31 AM
VulpesPosted Oct 7, 2014, 9:56 AM
List
EDIT:
If yList and y2 are lists of strings rather than ints, then use instead:
List
Hesham HassaneinPosted Oct 7, 2014, 9:44 AM
VulpesPosted Oct 7, 2014, 9:37 AM
1.Subtract the sum of the elements in the y2 list from the sum of those in yList; or
2. Subtract corresponding elements of the y2 list from those in yList and place the results in another list; or
3. Treat the elements in the y2 list as successive digits in an integer, do the same with the elements of yList and then subtract the two integers?
Hesham HassaneinPosted Oct 7, 2014, 9:19 AM
I want to subtract the elements from a list
List
i.e. 2 3 1 - 0 1 1 = 2 2 0
Is it possible using lists
Hesham HassaneinPosted Oct 7, 2014, 5:22 AM
I have this method that returns the yList
static List
{
StreamReader reader = File.OpenText(@"C:\Robotron\Execution\TS_Right_Bottom.csv");
string line4;
List
List
List
while ((line4 = reader.ReadLine()) != null)
{
string[] fields = line4.Split(';');
if (fields.Length != 15) continue;
if (fields[1] == "intMobileID") continue;
int id = Convert.ToInt32(fields[1]);
if (id == MobileID)
{
xList.Add(fields[5]);
yList.Add(fields[6]);
zList.Add(fields[7]);
}
}
reader.Close();
if (xList.Count() > 0)
{
Console.WriteLine("The Corresponding Right Bottom Coordinates are: ");
for (int i = 0; i < xList.Count; i++)
{
Console.WriteLine(" {0} : {1} : {2}", xList[i], yList[i], zList[i]);
}
}
else
Console.WriteLine("There is no corresponding Information");
Console.ReadKey();
return yList;
what i need is to get the xList and call it in another method
This method is below
static void Get_Length_Width(int MobileID)
{
List
}
do you know how can i do so?.. I took alook at Tuples function that returns two values but i wanna call this value in another method...
VulpesPosted Oct 6, 2014, 11:10 AM
One thing I've noticed that you're using a semi-colon for the field separator in one file but using a tab for the same purpose in the other file. Is this in fact correct?
Also are there definitely 15 fields per line in both files?
Hesham HassaneinPosted Oct 6, 2014, 8:20 AM
does this fill the values in x and y because the reason count is zero, I think because of x and y
List
List
y.Add(fields[6]);
Hesham HassaneinPosted Oct 6, 2014, 7:07 AM
VulpesPosted Oct 6, 2014, 7:03 AM
Is that a possibility?
Hesham HassaneinPosted Oct 6, 2014, 5:02 AM
The csv file only has the first row written and nothing else
intMobileID;strTasten_Name;strTastenfunktion;strDruckrichtung;dblX_Wert;dblY_Wert;dblZ_Wert;dbldX_Wert;dbldY_Wert;dbldZ_Wert;intTastendruck;intDauer;intAnzahl;strBewegungstyp
I really appreciate your help. man
VulpesPosted Oct 2, 2014, 11:46 AM
If you want to write multiple values to the file, then you'll need to use a loop.
There's also the question of what to do if you have more 'x' values than 'y' values, or vice versa.
For now, I've written the loop so that it only writes a line if there are both x and y values.
static void LeftBottom(int MobileID)
{
string ProgramPfad = (@"C:\Robotron\Execution\"+MobileID+".csv");
int b = 2;
string linee;
List
List
int z = 25;
StreamReader reader = File.OpenText(@"C:\Robotron\Execution\TS_Right_Bottom.csv");
while ((linee = reader.ReadLine()) != null)
{
string[] fields = linee.Split('\t');
if (fields.Length != 15) continue;
int Keyid = Convert.ToInt32(fields[0]);
int id = Convert.ToInt32(fields[1]);
if (id == MobileID)
{
y.Add(fields[6]);
//break; // remove this now
}
}
reader.Close();
int count = Math.Min(x.Count, y.Count);
string[] buffer = new string[count];
using (System.IO.StreamWriter file = new System.IO.StreamWriter(ProgramPfad))
{
file.WriteLine("intMobileID;strTasten_Name;strTastenfunktion;strDruckrichtung;dblX_Wert;dblY_Wert;dblZ_Wert;dbldX_Wert;dbldY_Wert;dbldZ_Wert;intTastendruck;intDauer;intAnzahl;strBewegungstyp");
for(int i = 0; i < count; i++)
{
buffer[i] = (Keyid+";"+MobileID+";"+"TS_Left_Bottom"+";"+";"+"mitte"+";"+ x[i]+";"+y[i] +";"+z+";"+0+";"+0+";"+0+";"+12000+";"+100+";"+1+";"+"XY-Z");
file.WriteLine(buffer[i]);
}
}
}
Hesham HassaneinPosted Oct 2, 2014, 11:31 AM
I want to display in the x column the whole xlist and in the ycolumn the whole ylist
so can i say
string[] buffer = new string[sizeof (x)];
but then
buffer[b - 1] = (Keyid + ";" + MobileID + ";" + "TS_Left_Bottom" + ";" + ";" + "mitte" + ";" + x + ";" + y + ";" + z + ";" + 0 + ";" + 0 + ";" + 0 + ";" + 12000 + ";" + 100 + ";" + 1 + ";" + "XY-Z");
I mean after displaying the headers in the first line of the csv the next line should be like the first one except the x and the y list should be displayed so that its going to be big csv file. you know what i mean?
Hesham HassaneinPosted Oct 2, 2014, 11:17 AM
{
file.WriteLine("intMobileID;strTasten_Name;strTastenfunktion;strDruckrichtung;dblX_Wert;dblY_Wert;dblZ_Wert;dbldX_Wert;dbldY_Wert;dbldZ_Wert;intTastendruck;intDauer;intAnzahl;strBewegungstyp");
string[] buffer = new string[1];
foreach (string strg in buffer)
{
file.WriteLine(strg);
}
buffer[b - 1] = (Keyid + ";" + MobileID + ";" + "TS_Left_Bottom" + ";" + ";" + "mitte" + ";" + x[0] + ";" + y[0] + ";" + z + ";" + 0 + ";" + 0 + ";" + 0 + ";" + 12000 + ";" + 100 + ";" + 1 + ";" + "XY-Z");
buffer[b] = (Keyid + ";" + MobileID + ";" + "TS_Left_Bottom" + ";" + ";" + "mitte" + ";" + x[1] + ";" + y[1] + ";" + z + ";" + 0 + ";" + 0 + ";" + 0 + ";" + 12000 + ";" + 100 + ";" + 1 + ";" + "XY-Z");
}
}
reader.Close();
Hesham HassaneinPosted Oct 2, 2014, 11:17 AM
intMobileID;strTasten_Name;strTastenfunktion;strDruckrichtung;dblX_Wert;dblY_Wert;dblZ_Wert;dbldX_Wert;dbldY_Wert;dbldZ_Wert;intTastendruck;intDauer;intAnzahl;strBewegungstyp
I want the second row to have the x and y values
VulpesPosted Oct 2, 2014, 10:59 AM
The LeftBottom method will then need changing as follows:
Hesham HassaneinPosted Oct 2, 2014, 10:50 AM
Hesham HassaneinPosted Oct 2, 2014, 10:44 AM
like this.. the other one is the same but with another file name and directory..
so how can i use the x from this method and the y from the other file to print it in the method of writing the csv
static void RightBottom(int MobileID)
{
StreamReader reader = File.OpenText(@"C:\Robotron\Execution\TS_Right_Bottom.csv");
string line4;
List
List
List
while ((line4 = reader.ReadLine()) != null)
{
string[] fields = line4.Split(';');
if (fields.Length != 15) continue;
if (fields[1] == "intMobileID") continue;
int id = Convert.ToInt32(fields[1]);
if (id == MobileID)
{
xList.Add(fields[5]);
yList.Add(fields[6]);
zList.Add(fields[7]);
}
}
reader.Close();
if (xList.Count() > 0)
{
Console.WriteLine("The Corresponding Coordinates are: ");
for (int i = 0; i < xList.Count; i++)
{
Console.WriteLine(" {0} : {1} : {2}", xList[i], yList[i], zList[i]);
}
}
else
Console.WriteLine("There is no corresponding Information");
Console.ReadKey();
}
Wim SturkenboomPosted Oct 2, 2014, 10:37 AM
Is there is a guarantee that line1 in file1 refers to the same mobile number as line 1 in file2 and that both files have the same amount of lines, you can read file2 in parallel. Something like
StreamReader reader = File.OpenText(@"C:\Robotron\Execution\TS_Right_Bottom.csv");
StreamReader rdr = File.OpenText(@"C:\Robotron\Execution\TS_Left_Top.csv");
while ((linee = reader.ReadLine()) != null)
{
string linefromfiletwo = rdr.ReadLine();
string fieldsfromfiletwo[] = linefromfiletwo.Split("\t");
string[] fields = linee.Split('\t');
if (fields.Length != 15) continue;
int Keyid = Convert.ToInt32(fields[0]);
int id = Convert.ToInt32(fields[1]);
if (id == MobileID)
{
x.Add(fields[5]);
//y.Add(fields[6]);
y.Add(fieldsfromfiletwo[6]);
break;
}
...
...
If there is no guarantee, you're better off reading both files separately and store the data in memory (e.g. in two datatables) and next process those tables.
Note that your approach to reading and writing csv files will cause problems if any of the fields in a record contains a '\t' (for the reading; you might have implemented the length check for that purpose, I'm not sure) or a semicolon (for the writing). There is a multitude of source code for csv readers and csv writers available on the web.
I have used the below (the first one is the writer):
http://knab.ws/blog/index.php?/archives/3-CSV-file-parser-and-writer-in-C-Part-1.html
http://knab.ws/blog/index.php?/archives/10-CSV-file-parser-and-writer-in-C-Part-2.html
Alternatively, you can use the (standard) TextFieldParser class for the reading.
Hesham HassaneinPosted Oct 2, 2014, 10:32 AM
VulpesPosted Oct 2, 2014, 10:10 AM