open text file and see how many lines are the number 1 or 2? C#

Discussion in 'Programming' started by Anveto, Nov 29, 2011.

  1. #1
    How would you open a text file and then see how many instances of a number there are

    I have a file like this

    1
    2
    2
    2
    2
    2
    1
    1
    1
    2
    2
    
    Code (markup):
    Where I want to use out how many times the number 1 and 2 occurs.
     
    Anveto, Nov 29, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Read each line. Increment a counter each time you read a line that's "1".
     
    Rukbat, Dec 1, 2011 IP
  3. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #3
    Delete all 2s, all LFs, and all CRs. Then you will have a line like "11111111". Length of line is number or 1s in the line.
     
    mmerlinn, Dec 2, 2011 IP