look im new in programming, would be awesome if u could help me.
Im reading a whole .csv File with this:
private void button3_Click(object sender, EventArgs e)
{
{
var content = string.Empty;
using (StreamReader reader = new StreamReader(@"C:/1.csv", System.Text.Encoding.Default))
{
content = reader.ReadToEnd();
reader.Close();
}
content = content.Replace("\"", string.Empty);
content = content.Replace(" ", "");
content = content.Replace(" ", "");
using (StreamWriter writer = new StreamWriter(@"C:/2.csv"))
{
writer.Write(content);
writer.Close();
}
}
}1.CSV Input looks like this:
" 09000";"Catalaog 1";10148;234
" 09001";"Catalaog 2";0;345
2.CSV Outlooks like this:
09000~Catalaog 1~10148~234
09001~Catalaog 2~0~0
I need a rewrite of my Code that the last to slots 10148~234 get calculated.
String must look like this at the end:
09000~Catalaog 1~9914~234
09001~Catalaog 2~0~0
See the diffrence? The 10148 - 234 = 9914. The 10148 gets replaced with the result.
Could u rewrite my upper code and help me ?:<
VulpesPosted Apr 5, 2013, 6:07 AM
s hPosted Apr 8, 2013, 3:56 AM
s hPosted Apr 5, 2013, 5:52 AM
Now other question, how can i add 15 spaces @ the start like this @ every Line:
09000";11131;476
09001";11132;472
VulpesPosted Apr 5, 2013, 4:58 AM
Also I'd check Task Manager to see whether there's still an instance of the current process running. If there is, kill it from there.
s hPosted Apr 5, 2013, 4:48 AM
string line;
bool first = true;
while ((line = reader.ReadLine()) != null)
{
if (first)
{
first = false;
continue;
}
string[] items = line.Replace("\"", "").Trim().Split(';');
if (items.Length != 3) continue;
int i1, i2;
bool b1 = int.TryParse(items[1], out i1);
bool b2 = int.TryParse(items[2], out i2);
if (!b1 || !b2) continue;
items[1] = Math.Max(0, i1 -i2).ToString();
line = String.Join("~", items, 0, 2);
writer.WriteLine(line);
}
But i have a Problem
The process can't access the file, because it is being used by another process
for StreamWriter
VulpesPosted Apr 5, 2013, 4:43 AM
However. try this (untested):
s hPosted Apr 5, 2013, 4:02 AM
how to Change ur Code that it works with this Input string:
" 09000";11131;476
VulpesPosted Apr 4, 2013, 3:14 PM
BASCHDIPosted Apr 4, 2013, 2:27 PM
this one "Do you want the subtraction to give 0 rather than a negative figure?"
VulpesPosted Apr 4, 2013, 11:57 AM
VulpesPosted Apr 4, 2013, 11:37 AM
1. Skip them altogether; or
2. Treat the "" as though it were 0?
s hPosted Apr 4, 2013, 11:03 AM
" 09000";"Catalog 1";11131;476
" 09000";"Catalog 2";11131;200
" 09000";"Catalog 3";11131;32
" 09000";"Catalog 4";11131;244
" 09001";"Catalog 5";0;""
" 09002";"Catalog 6";166;""
if there is nothing in it, there r only "" .
i get a Format Execption @
int i2 = int.Parse(items[3]);
what can i do ?
s hPosted Apr 4, 2013, 9:20 AM
VulpesPosted Apr 4, 2013, 9:19 AM
line = String.Join("~", items);
with this:
line = String.Join("~", items, 0, 3);
VulpesPosted Apr 4, 2013, 9:13 AM
s hPosted Apr 4, 2013, 9:00 AM
there is only 1 invalid line, at the start..rest goes like this:
"";"";0
" 09000";"catalog 1";10148;524
" 09001";"catalog 2";0;5225
" 09002";"catalog 3";166;522
" 09003";"catalog 4";557;4254
" 09004";"catalog 5";0;422
" 09005";"catalog 6";382;2424
" 09006";"catalog 7";0;42425
the diggits (last column) r randomly by me for now, cause no export.
VulpesPosted Apr 4, 2013, 8:55 AM
" 09001";"Catalaog 2";0;345
as subtracting 345 from 0 gives -345.
Do you want the subtraction to give 0 rather than a negative figure?
Also do you want any invalid lines to be just skipped and not written to the output file?
s hPosted Apr 4, 2013, 8:36 AM
and i get outofindex exeption, i need to skip the first line, cause its everytime filled with crap
"";"";0
" 09000";"Catalaog 1";10148;234
" 09001";"Catalaog 2";0;345
" 09002";"Catalaog 3";10148;234
" 09003";"Catalaog 4";0;345
s hPosted Apr 4, 2013, 8:11 AM
09000~Catalaog 1~10148~234
09001~Catalaog 2~0~0
09003~Catalaog 3~2000~300
whole file the last 2 delimited columns must b calculated like
09000~Catalaog 1~10148~234: 10148-234= 9914
09001~Catalaog 2~0~0: 0-0= 0
09003~Catalaog 3~2000: 2000-300 = 1700
File @ the end must look like this :
09000~Catalaog 1~9914~234
09001~Catalaog 2~0~0
09003~Catalaog 3~1700~300
can i do this with your code?
and thanks for the time u took for me, awesome!
VulpesPosted Apr 4, 2013, 7:47 AM