is there an existing library for counting characters?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fressanme
    New Member
    • Apr 2007
    • 36

    is there an existing library for counting characters?

    i would like to ask if there is an existing algorithm for counting characters from a text file?

    if there is,can i have a sample?
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    There is an algorithm - open the file, get a line, count each character in the file while the character is not the EOF char.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      If you're running a POSIX compliant system (MS Windows claims it is) just have
      a look at the fstat() function; that's all you need then; no need for reading
      the entire file and counting every single byte.

      kind regards,

      Jos

      Comment

      • fressanme
        New Member
        • Apr 2007
        • 36

        #4
        Originally posted by sicarie
        There is an algorithm - open the file, get a line, count each character in the file while the character is not the EOF char.
        how do i know its an EOF?what do i tell the program?and how should i put it if its the EOL char and i want the reader to go down to the next line?

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          how do i know its an EOF?
          Depends on the language and how you read from the file. You neglected to mention what language you are using....on a programming question.

          You can always find out how to determine EOF on your own. The languages and their standard libraries are documented. That means you can just Google up the documentation and read how to detect EOF.

          Once you figure out the logic to detecting an EOF, obviously write the appropriate code for it.

          and how should i put it if its the EOL char and i want the reader to go down to the next line?
          Lines are nothing but blocks of text marked at the end by a newline character. The reader has no concept of "down". Computers are not humans. They don't open up a file on Microsoft windows and stare at a monitor. They see a continous stream of bytes of the computer, and interpret them as you tell them to.

          Code:
          This is a line.\nThis is another line.\n\nNow a new paragraph.\nWith a second line.\nEOF
          There's 86 "characters " on that example above. But you would actually omit the '\n' newline characters and EOF from your calculation.

          But again, the actual logic you need to code depends on the language and what I/O functions you use to read the file.

          Comment

          • fressanme
            New Member
            • Apr 2007
            • 36

            #6
            Originally posted by oler1s
            Depends on the language and how you read from the file. You neglected to mention what language you are using....on a programming question.

            You can always find out how to determine EOF on your own. The languages and their standard libraries are documented. That means you can just Google up the documentation and read how to detect EOF.

            Once you figure out the logic to detecting an EOF, obviously write the appropriate code for it.

            Lines are nothing but blocks of text marked at the end by a newline character. The reader has no concept of "down". Computers are not humans. They don't open up a file on Microsoft windows and stare at a monitor. They see a continous stream of bytes of the computer, and interpret them as you tell them to.

            Code:
            This is a line.\nThis is another line.\n\nNow a new paragraph.\nWith a second line.\nEOF
            There's 86 "characters " on that example above. But you would actually omit the '\n' newline characters and EOF from your calculation.

            But again, the actual logic you need to code depends on the language and what I/O functions you use to read the file.
            this is a C++ forum so im using this kind of language..i guess theres no need to mention that.. :D

            thanks for the info..i'll try working on it right away..thanks a lot.. :)

            Comment

            Working...