hi guys
i have a text file with the contents as follows
code:x:y
2346:1:0
3456:2:6
2345:3:7
4567: 4:1
3456:5:3
i need to read from beginning of file and check if "4567"is present in file ,if present i need to move to that matched row 'y' column and increment the value....that is increment 1 in my above file and write it back in the same location.
please help
thanks
theLizardPosted Mar 31, 2016, 3:49 PM
string[] text = File.ReadAllLines(@"c:\temp\test.txt");
string[] values;
int value = -1;
for(int i = 0; i < text.Length; i++)
{
if(text[i] == "4567: 4:1")
{
values = text[i].Split(':');
int.TryParse(values[values.Length - 1], out value);
if(value != -1)
dirty = true;
value += 1;
text[i] = text[i].Substring(0, text[i].LastIndexOf(':')+1) + value.ToString();
break;
}
}
if(dirty)
File.WriteAllLines(@"c:\temp\test.txt", text);
brian dsouzaPosted Mar 31, 2016, 1:29 AM
Nishant MittalPosted Mar 31, 2016, 12:43 AM