临时数组我问这个之前,但大多数人并不明白我的问题。写作从文本文件
我有两个文本文件。 Gamenam.txt
这是我正在阅读的文本文件,以及gam enam_2.txt
。
在gamenam.txt
我有一个字符串是这样的:
01456
02456
05215
05111
01421
03117
05771
01542
04331
05231
我已经写了代码to count number of times substring "05" appears in text file before substring "01"
。
我被写入到gamenam_1.txt
输出是:
01456
02456
05215
05111
2
01421
03117
05771
1
01542
04331
05231
1
这是我写的,实现
string line;
int counter = 0;
Boolean isFirstLine = true;
try
{
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) == "01")
{
if (!isFirstLine)
{
sw.WriteLine(counter.ToString());
counter = 0;
}
}
if (line.Substring(0, 2) == "05")
{
counter++;
}
sw.WriteLine(line);
if (sr.Peek() < 0)
{
sw.Write(counter.ToString());
}
isFirstLine = false;
}
sr.Close();
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Exception finally block.");
}
该代码是可以正常使用的代码。
现在我必须编写代码在写入行之前打印子串“05”的计数。
我的输出应该是这个样子:
2
01456
02456
05215
05111
1
01421
03117
05771
1
01542
04331
05231
显然我应该先写行临时字符串数组,计数,然后编写计数,然后从后我临时数组写入线。
我新的发展,使我坚持试图找出如何我做到这一点。
任何帮助将高度赞赏。
打印? –
附注 - 如果你有回答你刚才的问题,然后确保你接受的答案@SergeyBerezovskiy我非常感激这是最有用的,你 –
。对于每个人的投入,因为我已经指出 – Brian02