Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

how to count word?


  • Please log in to reply
6 replies to this topic

#1 lia_hasya

lia_hasya

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 06 March 2012 - 07:49 AM

hi all..
i'm a new member..
may i ask about some coding?
By using the system call read() and write(),
do the coding to calculate the number of characters in the file data.txt
I have try to build the coding but I have no success..
Thanks
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 06 March 2012 - 01:03 PM

What language are you coding in?

Since this is in the Linux Programming and Scripting forum, I'll assume you're scripting using a command line shell. Use the Linux command 'wc' to count various entities in a file (words, letters, lines, etc.)

man page for wc:

NAME

       wc - print the number of newlines, words, and bytes in files

SYNOPSIS

       wc [OPTION]... [FILE]...

DESCRIPTION

       Print newline, word, and byte counts for each FILE, and a total line if
       more than one FILE is specified.	 With no FILE, or when FILE is -, read
       standard input.

       -c, --bytes
	      print the byte counts

       -m, --chars
	      print the character counts

       -l, --lines
	      print the newline counts

       -L, --max-line-length
	      print the length of the longest line

       -w, --words
	      print the word counts

       --help display this help and exit

       --version
	      output version information and exit

  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 lia_hasya

lia_hasya

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 07 March 2012 - 02:53 AM

thanks..
your answer seems too advance..
actually, I had not learned that language..
what I need is in C programming language..
can anybody help me?
thanks again
  • 0

#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 07 March 2012 - 06:29 AM

This should go in the C programming forum then.

To count characters in a text file, simply open the file to be read, and continue reading characters until you reach End Of File, incrementing a counter variable after each read. At the end, the counter variable will tell you how many characters are in the file.

The library you'll need is:
cstdio (stdio.h) - C++ Reference
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5 harryngh

harryngh

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 24 March 2015 - 10:31 PM

I like the Character Count tool. It works like a charm and has a lot of features.


  • 0

#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 25 March 2015 - 07:11 AM

Let's start with reading and writing files in C, for that Codecall provides a basic tutorial.

 

Counting is just about measuring the input, and it depends on how did you chose to read the file. Make me know  how you did it so we continue.


  • 0

#7 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts
  • Programming Language:C, C++, C#, Perl, Visual Basic .NET, Assembly, VBScript, Others

Posted 03 April 2015 - 06:49 PM

You can easily use functions like fseek() and ftell() to get the number of bytes in the file. So I suppose this is a good time to ask if the file is meant to be read as 1 byte per char, or is the encoding something like UTF-16?


  • 0

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download