Reputation: 83
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.
Upvotes: 0
Views: 271
Reputation: 83
Thanks for all you r help guys, from all you input, I finally got the output I needed.
I wrote my code like this
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();
}
Upvotes: 0
Reputation: 2186
Here's code for using a READ AHEAD implementation. Just before the end of the loop, you read the next line, if it is null (end of file) or it starts with "05" then you output and reset counter
try
{
int counter = 0;
//Pass the file path and name to the StreamReader constructer
StreamReader sr = new StreamReader("gamenam.txt");
//Pass the file path and name to the StreamReader constructer
StreamWriter sw = new StreamWriter("gamenam_1.txt");
string line = sr.ReadLine();
while (line != null)
{
if (line.Substring(0, 2) == "03")
{
counter++;
}
sw.WriteLine(line);
line = sr.ReadLine();
if ((line == null) || (line.StartsWith("05")))
{
sw.WriteLine(counter.ToString());
counter = 0;
}
}
//Close
sr.Close();
sw.Close();
}
//Catching exception
catch (Exception e)
{
//Exception Message
Console.WriteLine("Exception: " + e.Message);
}
}
finally
{
Console.WriteLine("Exception finally block.");
}
Upvotes: 1
Reputation: 186688
Instead of your while loop
you can try something like that:
...
Boolean isFirstLine = true;
while ((line = sr.ReadLine()) != null) {
// If line starts with "05" we should print out counter
// (that is number of "03" started lines)
// unless it is the first line in the file
if (line.StartsWith("05")) {
if (!isFirstLine)
sw.WriteLine(counter.ToString());
sw.WriteLine(line);
counter = 0;
isFirstLine = false;
continue;
}
sw.WriteLine(line);
if (line.StartsWith("03"))
counter += 1;
// We should also print out counter after the last file line
// if, say, counter > 0
if (sr.Peek() < 0) // <- No next line
if (counter > 0)
sw.WriteLine(counter.ToString());
isFirstLine = false;
}
...
Upvotes: 2