470,571 Members | 2,360 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

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?
Feb 2 '08 #1
5 1424
sicarie
4,677 Expert Mod 4TB
There is an algorithm - open the file, get a line, count each character in the file while the character is not the EOF char.
Feb 2 '08 #2
JosAH
11,448 Expert 8TB
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
Feb 2 '08 #3
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?
Feb 2 '08 #4
oler1s
671 Expert 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 2 '08 #5
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.

Expand|Select|Wrap|Line Numbers
  1. 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.. :)
Feb 3 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Srinath Avadhanula | last post: by
6 posts views Thread by shablool | last post: by
5 posts views Thread by Matt | last post: by
3 posts views Thread by arnuld | last post: by
4 posts views Thread by Herman.Schultz | last post: by
23 posts views Thread by June Lee | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.