Getting the word count of a file

This is a discussion on Getting the word count of a file within the C++ Programming forums, part of the General Programming Boards category; Is there any way to get the total number of words that is in a text file without looping through ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Getting the word count of a file

    Is there any way to get the total number of words that is in a text file without looping through each line and breaking it down into words and keeping track of that number with a counter variable? Something like dividing the size of the file with the size of a string?
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How long is a word? Find that value, then you can divide the file size.

    Otherwise, you're stuck with counting.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That's like asking if there's a way to win the lottery without buying a ticket for every single combination of numbers.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > Something like dividing the size of the file with the size of a string?
    I'm sure you can come up with an approximate answer. It depends what you intend to do with the result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Word Count
    By simple in forum C Programming
    Replies: 12
    Last Post: 10-04-2002, 10:47 PM

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