Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get the count of lines in .txt file

How to get the count of lines in .txt file

Scheduled Pinned Locked Moved C#
csharphelptutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodeItWell
    wrote on last edited by
    #1

    How to get the count of lines in .txt file. Help.

    C#

    E D S 3 Replies Last reply
    0
    • C CodeItWell

      How to get the count of lines in .txt file. Help.

      C#

      E Offline
      E Offline
      Edmundisme
      wrote on last edited by
      #2

      I'm not sure if you can do anything but brute force it. Here's one way: // Read the file into a byte array FileStream fs = File.Open("TextFile1.txt", FileMode.Open); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); // int to store num of lines int numOfLines = 0; // if the file is not empty, we have at least 1 line if (bytes.Length > 0) numOfLines++; // increment the count for each newline character found foreach (byte b in bytes) if(b == '\n') numOfLines ++; ... This is just a starting point and would need to be more robust depending on the requirements. If the textfile's length is greater than int.MaxValue I suppose you'd need to read the file in sections. This solution will count all lines (even blank lines and trailing blank lines). I would be interested to see another solution. I'm sure there must be a better one. Cheers, Ian

      W 1 Reply Last reply
      0
      • C CodeItWell

        How to get the count of lines in .txt file. Help.

        C#

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        That depends on what you define as a line. There is no single method you can call to just return this value. You'd have to write a function that counts the number of lines in the file, according to the rules that you define for a "line", by reading the entire file and counting the lines up, one-by-one.

        Dave Kreskowiak Microsoft MVP - Visual Basic

        1 Reply Last reply
        0
        • E Edmundisme

          I'm not sure if you can do anything but brute force it. Here's one way: // Read the file into a byte array FileStream fs = File.Open("TextFile1.txt", FileMode.Open); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); // int to store num of lines int numOfLines = 0; // if the file is not empty, we have at least 1 line if (bytes.Length > 0) numOfLines++; // increment the count for each newline character found foreach (byte b in bytes) if(b == '\n') numOfLines ++; ... This is just a starting point and would need to be more robust depending on the requirements. If the textfile's length is greater than int.MaxValue I suppose you'd need to read the file in sections. This solution will count all lines (even blank lines and trailing blank lines). I would be interested to see another solution. I'm sure there must be a better one. Cheers, Ian

          W Offline
          W Offline
          Wayne Phipps
          wrote on last edited by
          #4

          I would personally prefer to use StreamReader, it certainly looks more elegant.

                      int lineCount = 0;
                      // Create an instance of StreamReader to read from a file.
                      // The using statement also closes the StreamReader.
                      using (System.IO.StreamReader sr = new System.IO.StreamReader("myFile.txt")) 
                      {
                          // Read and display lines from the file until the end of the file is reached.
                          while ( sr.ReadLine() != null) lineCount++;
                      }
                      Console.WriteLine( "Line Count: " + lineCount.ToString() );
          

          Obviously you should add some error checking like making sure the file exists ETC but that should otherwise do the job. Hope it helps

          Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog

          E 1 Reply Last reply
          0
          • W Wayne Phipps

            I would personally prefer to use StreamReader, it certainly looks more elegant.

                        int lineCount = 0;
                        // Create an instance of StreamReader to read from a file.
                        // The using statement also closes the StreamReader.
                        using (System.IO.StreamReader sr = new System.IO.StreamReader("myFile.txt")) 
                        {
                            // Read and display lines from the file until the end of the file is reached.
                            while ( sr.ReadLine() != null) lineCount++;
                        }
                        Console.WriteLine( "Line Count: " + lineCount.ToString() );
            

            Obviously you should add some error checking like making sure the file exists ETC but that should otherwise do the job. Hope it helps

            Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog

            E Offline
            E Offline
            Edmundisme
            wrote on last edited by
            #5

            Thanks. I knew there had to be a better way.

            1 Reply Last reply
            0
            • C CodeItWell

              How to get the count of lines in .txt file. Help.

              C#

              S Offline
              S Offline
              sherifffruitfly
              wrote on last edited by
              #6

              What counts as a "line"? A certain non-printing character? (Then just count the number of that character.) A certain line length? (Then take (total chars)/(line length)). Etc.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups