Funktion that returns the number of lines

This is a discussion on Funktion that returns the number of lines within the C++ Programming forums, part of the General Programming Boards category; Does someone know a C++ funktion that returns the number of lines of a .txt File?...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    21

    Funktion that returns the number of lines

    Does someone know a C++ funktion that returns the number of lines of a .txt File?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Nope don't know of any funktions. Though it wouldn't be hard to create a function to do it.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Do excuse Thantos, I think he forgets that you are from austria and spelling function they way its spelled there.

    I prefer the C way of reading files so I'll give a C example.

    Code:
    #include <cstdio>
    #include <iostream>
    
    // This function return the number of lines or -1 if the file doesn't exist
    long lineCount(const char *filename) {
      long count = -1;
      FILE *file;
      char buffer[4096];
    
      if((file = std::fopen(filename, "r"))) {
        do {
          count++;
        } while(std::fgets(buffer, 4096, file));
    
        std::fclose(file);
      }
    
      return count;
    }
    
    int main(int argc, char **argv) {
      if(argc == 2) {
         std::cout << argv[1] << " contains " << lineCount(argv[1]) <<  " lines" << std::endl;
      }
    
      return 0;
    }
    Of course the above is simplistic and doesn't handle lines above 4k, but its a start.

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    About size of the whole file

    Is there any way of discovering the size of the file? I know that I can read until EOF, in partial buffers, I am just curious about such a function.

    Thanks any help.
    Nothing more to tell about me...
    Happy day =)

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by gustavosserra
    Is there any way of discovering the size of the file? I know that I can read until EOF, in partial buffers, I am just curious about such a function.

    Thanks any help.
    http://cboard.cprogramming.com/searc...searchid=77170

    Its been answered before, IIRC its not that difficult

    Do excuse Thantos, I think he forgets that you are from austria and spelling function they way its spelled there.
    C++ is C++ regardless of any written or spoken language the user may use.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    actually, my c++ teacher is from sudan or someplace lie that and she says there is c++ for different (human) languages

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If so then color me wrong

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    pfft!....i would need the 124 crayon set for that....i only have the 36

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can borrow my nephews

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Oh... Crayons?! Where!
    You'll be able to find system specific ways to get the size of the file without opening it and reading the whole thing, but to find the number of lines, you will need to open it (as you need to actually see what is inside).

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    C/C++ itself (the actual code portion) will be the same universally. I'm speaking more in terms of how the word function is spelled may vary from language to language (spoken language, that is).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-20-2008, 11:58 PM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  4. Number of words multiplied by the number of lines in Linux
    By sjalesho in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 02:25 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21