I have a text file called info.txt with string in it.
info.txt
05331
02555
03211
05222
04321
02387
03444
03127
05117
03680
03881
01579
03111
My output should be in new.txt
Output new.txt
05331
02555
03211
1
05222
04321
02387
03444
03127
2
05117
03680
03881
01579
03111
3
Basically I should get the count of all strings starting with "03" and print the count before the substring "01"
try
{
String line;
Int counter =0;
StreamReader sr = new StreamReader("C:\\Files\\gamenam.txt");
StreamWriter sw = new StreamWriter("C:\\Files\\gamenam_1.txt");
while ((line = sr.ReadLine())!= null)
{
if (line.substring(0,2) == "05")
{
sw.Write(counter.ToString());
counter =0;
}
If (line.subString(0,2) =="03")
{
//loop
counter++;
}
sw.WriteLine(line);
}
sr.Close();
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Exception finally block.");
}
}
After writing my code I could only get.
0
05331
02555
03211
1
05222
04321
02387
03444
03127
2
05117
03680
03881
01579
03111
The 0 on the first line shouldn't be the since I have no stings before and there is no count for last count.
Please help guys.
01but in expected output you print it before0505line will be last in input file? Also should you write count each time05is found, or only if there was03between them?