LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2003, 04:17 AM   #1
cxel91a
Member
 
Registered: May 2001
Posts: 61

Rep: Reputation: 15
separating words


I'm working on this project to find the number count for a specific word in a file.
I need to put each word into an array, but I'm having problems setting it up.
Can someone help me. This is my own personal project, NOT school homework.

//Program to count words in a file

#include<iostream>
using namespace std;
#include<fstream>
#include<cstdlib>
#include<cstring>


int main(int argc, char* argv[])
{
if ( argc <= 2)
{
cout<<"Usage: program filename arg1 \n";
exit(1);
}

ifstream inFile;
if(!inFile)
{
cout<<"Error cannot create inFile object\n";
exit(1);
}

inFile.open(argv[1]);
if(inFile.fail())
{
cout<<"Error cannot open file\n";
exit(1);
}

inFile.seekg(0L,ios::beg);

char buffer[80];
while ( inFile.peek() != EOF)
{
inFile.getline(buffer,80);
cout<<buffer<<endl;

// Problems here <-

}


return 0;
}

Last edited by cxel91a; 04-27-2003 at 04:21 AM.
 
Old 04-27-2003, 09:58 AM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Why not use perl. You could do the count in a line.
 
Old 04-27-2003, 06:59 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
How do you like this aproach? :)

Not quite using an array, but very
cplusplussy ;)

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

map<string, int> count;
string w;

int main(int argc, char* argv[])
{
  cout << endl << argc << endl;
  if ( argc < 2)
    {
      cout<<"Usage: program filename arg1 \n";
      exit(1);
    }

  ifstream inFile;
  if(!inFile)
    {
      cout<<"Error cannot create inFile object\n";
      exit(1);
    }

  inFile.open(argv[1]);
  if(inFile.fail())
    {
      cout<<"Error cannot open file\n";
      exit(1);
    }

  inFile.seekg(0L,ios::beg);

  while ( inFile >> w )
    {
    ++count[w];
    }

  for (map<string, int>::const_iterator it=count.begin();
       it != count.end() ; ++it)
    {
      cout << it->first << "\t" << it->second << endl;
    }

  return 0;
}

Cheers,
Tink

Last edited by Tinkster; 04-27-2003 at 07:01 PM.
 
Old 04-27-2003, 08:34 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
Originally posted by david_ross
Why not use perl. You could do the count in a line.
More elegant than that? :)
Code:
  while ( inFile >> w )
    {
    ++count[w];
    }
STL owns you ;)


Cheers,
Tink
 
Old 04-28-2003, 01:44 AM   #5
cxel91a
Member
 
Registered: May 2001
Posts: 61

Original Poster
Rep: Reputation: 15
I tried to compile your code but it will not work, but I'm looking into the STL you provided.
 
Old 04-28-2003, 01:53 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Compiles here ... and without using STLPort

What compiler are you using, what
error message do you get?

Cheers,
Tink
 
Old 04-28-2003, 03:33 AM   #7
cxel91a
Member
 
Registered: May 2001
Posts: 61

Original Poster
Rep: Reputation: 15
I'm using
c++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
I also changed the header file for map to map.h

These are the errors with #include <map.h>

In file included from /usr/include/c++/3.2/backward/map.h:59,
from countwords.cpp:4:
/usr/include/c++/3.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
countwords.cpp: In function `int main(int, char**)':
countwords.cpp:38: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std:air<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
countwords.cpp:41: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std:air<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
countwords.cpp:42: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std:air<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here


These errors with #include <map>

countwords.cpp: In function `int main(int, char**)':
countwords.cpp:38: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std:air<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
countwords.cpp:41: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std:air<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
countwords.cpp:42: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std:air<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
 
Old 04-28-2003, 02:28 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
gcc 2.95.3 here :}

Guess I'll have to re-check my syntax if 3.2 is
nagging about it :D

Cheers,
Tink
 
Old 04-28-2003, 02:49 PM   #9
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Quote:
Originally posted by Tinkster
More elegant than that?
Code:
  while ( inFile >> w )
    {
    ++count[w];
    }
STL owns you


Cheers,
Tink
Not bad - I still prefer something in perl:
Code:
#!/usr/bin/perl

while(<STDIN>){$count += $_ =~ s/$ARGV[0]//g}
print "There are $count occurences of $ARGV[0]\n";

exit;
Call it using "cat myfile | wc.pl STRING". (Assuming wc.pl is in your path, your file is called "myfile" and you want to search for STRING)
 
Old 04-28-2003, 03:27 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
I want to know how often each word was in myfile :)

Code:
   //the most amazing thing is that this small loop
   //does the entire counting/splitting up ;)
  while ( inFile >> w )
    { 
    ++count[w];
    }

   //that one just does the output :)
   //of the words & counts
  for (map<string, int>::const_iterator it=count.begin();
       it != count.end() ; ++it)
    {
      cout << it->first << "\t" << it->second << endl;
    }
Cheers,
Tink
 
Old 04-28-2003, 03:55 PM   #11
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Quote:
Originally posted by Tinkster
I want to know how often each word was in myfile
Then use this:

Code:
#!/usr/bin/perl

# Read the data from STDIN
while(<STDIN>){$string .= $_}

# Run through each word and increment an associative array based on the word
foreach $word (split(/\s|\.|\?|\!|\"|\'|\(|\)|\:|\;/,$string)){if($word){$count{$word}++}}

# Sort the array then print the word count
foreach $key (sort keys %count){print "$key = $count{$key}\n"}

exit;
Only one more line than before!

Anything else?
 
Old 04-28-2003, 05:31 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
/me shudders:"Take it away! It hurts us!!" :D

Cheers,
Tink
 
Old 04-29-2003, 10:01 AM   #13
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Rep: Reputation: 30
cat /path/filename |grep -i lookupword |wc -l

Can it be any more easy??
 
Old 04-29-2003, 10:03 AM   #14
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Rep: Reputation: 30
Oops, sorry, should read better, missed the array part.....
 
Old 04-29-2003, 01:05 PM   #15
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
It also doesn't take punctuation into account eg

"hello" != "hello."
"blah!" != "blah"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
separating a comma delimited line mrobertson Programming 7 07-27-2005 01:56 PM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 07:48 PM
separating strings cxel91a Programming 3 05-01-2004 06:54 PM
separating syslog output by application mscott Linux - Software 0 03-08-2004 11:45 AM
reason for separating web server and database server hardigunawan Linux - Security 5 01-25-2003 12:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration