By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

is there an existing library for counting characters?

P: 36
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
Share this Question
Share on Google+
5 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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

Expert 10K+
P: 11,448
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

P: 36
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

Expert 100+
P: 671
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

P: 36
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.