0

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.

14
  • 3
    Have read your samples twice and still can't get logic of numbers you trying to get. You say you want to print count before substring 01 but in expected output you print it before 05 Commented Jan 30, 2014 at 7:38
  • As the first line of the file begins 05 it immediately writes out the counter. Basically, you need to read AHEAD, rather than BEHIND, that is, you need to know what the NEXT line is before deciding to write out the counter. Commented Jan 30, 2014 at 7:45
  • According to the output you provided it looks like the logic is that you print the count each time the string starting with "05" appears AND at the end of the file. Is this correct? Commented Jan 30, 2014 at 7:46
  • @SergeyBerezovskiy sorry for that, I meant 05 Commented Jan 30, 2014 at 7:54
  • @user3251829 is it possible that 05 line will be last in input file? Also should you write count each time 05 is found, or only if there was 03 between them? Commented Jan 30, 2014 at 7:56

3 Answers 3

2

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;
}
...
Sign up to request clarification or add additional context in comments.

Comments

1

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.");
        }

Comments

0

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();
       }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.