C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 4th, 2005, 08:04 AM
karahasan karahasan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 10 karahasan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 21 sec
Reputation Power: 0
input/output questions and keyword searches

Im currently making a program (finder.exe) that looks for desired keywords and counts how many times it was "seen."

2 questions:
1) The specs require that I access it from command prompt.
Ex. C:\finder fox myFile.txt, where fox is the keyword, and myFile.txt is the file the program searches.
How do I "convert" fox and myFile.txt into cin and instream respectively?

2) When writing the function that actually finds the words, will recursion do the trick? If not, any alternatives?

Reply With Quote
  #2  
Old October 4th, 2005, 08:45 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
1) Accessing command line parameters:
To read the command line from a C program, your main() function needs to have two parameters, the first an integer, the second a pointer to an array of character arrays (strings). The integer parameter will then contain the number of parameters passed, and the array each parameter (the parameter seperators is any whitespace character, space, tab, etc).

The following example will simply print each parameter you've passed to the program:
cpp Code:
Original - cpp Code
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])        // not the two parameters, an integer and an array of constant strings
  4. {
  5.     // declarations
  6.     int i;
  7.  
  8.     // read all parameters and print them to stdout
  9.     for(i = 0; i < argc; i++)
  10.         printf("Argument nr. %d is: %s\n", i, argv[i]);
  11.  
  12.     // exit
  13.     return 0;
  14. }

Note that argument 0 is always the command you gave to start the program (so usually the executable's filename).
As for converting these to cin and instream, you can directly convert the first parameter to a string:
Code:
searchword = argv[1];
and to read the file, just use
Code:
ifstream fileIn(argv[2]);


2) Wouldn't recommend it. If the file's too big, you might actually run into memory problems.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Last edited by Itsacon : October 4th, 2005 at 08:52 AM.

Reply With Quote
  #3  
Old October 4th, 2005, 09:10 AM
karahasan karahasan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 10 karahasan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 21 sec
Reputation Power: 0
Thanks man! At least I know I have to work with strings when making that function.

Anyone know how to turn the printf line into a cout one? Im using C++

Reply With Quote
  #4  
Old October 4th, 2005, 09:16 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
printf works fine in C++, if you use the right library (cstdio I believe), but conversion is easy:
cpp Code:
Original - cpp Code
  1. cout << "argument nr. " << i << " is: " << argv[i] << "\n";

Reply With Quote
  #5  
Old October 5th, 2005, 04:42 AM
karahasan karahasan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 10 karahasan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 21 sec
Reputation Power: 0
Ok, Im almost done with the program. However, I stumbled upon a few problems I cannot understand:

1) clrscr() is undeclared.
2) invalid conversion in lines 42 and 49.
3) lines 42 and 49: "initializing argument 1 of 'int getc(FILE*)'"

Code:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;



int index=0;     /*   number of letters of keyword */
int count=0;     /*   number of found keywords */

main(int argc, char *argv[])
{
   int n=0;
   int wordlength=0;
   int count;
   char letter;
   FILE *fileptr;
   clrscr();

   if (argc != 3)  
   {
		      /* error msg */
      cout << "Error! Unexpected number of arguments.\n";
      cout << "Usage " << argv[0] << "<filename> <keyword>\n";
   }
   
   else  
   {
      ifstream fileptr(argv[1]);
      
      ofstream fout;
      fout.open("counter.txt");
      
      
      wordlength=strlen(argv[2]);

      do  
      {
	    letter = getc(fileptr);
        for (n=0; n < wordlength; n++) 
        {
            if (letter==argv[2][n])  
            {
	        index++;
	         letter = getc(fileptr);
             }
             
	        else  
            {
             index=0;
            }
         }
         
	     if(index==wordlength)  
         {
         count++;
	     index=0;
	     }

      } 
      while(letter!= EOF);
   
   fout << "FILE:" << argv[0] << endl;
   fout << "SEARCHKEY:" << argv[2] << endl;
   fout << "OCCURENCES" << count << endl;
}

return 0;
}

Reply With Quote
  #6  
Old October 5th, 2005, 04:49 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
Quote:
Originally Posted by karahasan
Ok, Im almost done with the program. However, I stumbled upon a few problems I cannot understand:

1) clrscr() is undeclared.
2) invalid conversion in lines 42 and 48.
3) lines 42 and 48: "initializing argument 1 of 'int getc(FILE*)'"

  1. clrscr() requires the <conio.h> library.
  2. and 3. getc() returns an integer, but you try to put it into a char. Either cast it to a char:
    Code:
    letter = (char) getc(fileptr);

    or make letter an integer.

Reply With Quote
  #7  
Old October 5th, 2005, 04:54 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 41 m 4 sec
Reputation Power: 4
An ifstream is not a FILE *..

Use one of the methods of ifstream (the stream operator may also work, I'm not sure about that)

http://www.cplusplus.com/ref/iostream/ifstream/

Reply With Quote
  #8  
Old October 5th, 2005, 04:57 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 41 m 4 sec
Reputation Power: 4
Stream operator does work (which is also a method of ifstream, an overloaded operator in c++ lingo.. method is java lingo sorry..)

Example:
cpp Code:
Original - cpp Code
  1.  
  2. // using ifstream constructors.
  3. #include <iostream>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. int main () {
  8.  
  9.   ifstream infile ("test.txt");
  10.  
  11.   char bla;
  12.  
  13.   for( infile >> bla ; !infile.eof(); infile >> bla )
  14.   {
  15.     printf( "%c\n", bla );
  16.   }
  17.  
  18.   infile.close();
  19.  
  20.   return 0;
  21. }

Last edited by Icon : October 5th, 2005 at 06:47 AM. Reason: Now the example makes sense :)

Reply With Quote
  #9  
Old October 5th, 2005, 05:20 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
Note that there's a small error in your output code:
Code:
 
   fout << "FILE:" << argv[0] << endl;
   fout << "SEARCHKEY:" << argv[2] << endl;
   fout << "OCCURENCES" << count << endl;


I suppose in the first line you want to output the name of the file that was seached, so it should be argv[1], not argv[0].

Also, I notice you've inverted the order of the parameters since your first post (then is was keyword first, then filename, now it's the other way around. I prefer the old way (like grep) especially since you can easily make your code more nifty like this:
cpp Code:
Original - cpp Code
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. //#include <conio.h>        note that conio is not ANSI-C, so I removed it and the clrscr() call for now
  7.  
  8. using namespace std;
  9.  
  10.  
  11. // Why is this in the global scope, and why do you declare count twice?
  12. //int index=0;     /*   number of letters of keyword */
  13. //int count=0;     /*   number of found keywords */
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17.     int i, n=0;   // added loop variable i
  18.     int wordlength=0;
  19.     int count, index;
  20.     char letter;
  21.     FILE *fileptr;
  22.     // clrscr();        // see above
  23.  
  24.     if (argc < 3)    // Let there be AT LEAST 3 arguments
  25.     {
  26.         /* error msg */
  27.         cout << "Error! Unexpected number of arguments.\n";
  28.         cout << "Usage " << argv[0] << " <keyword> <filename> [<filename>]\n";   // note reversed order of arguments, and optional 4th (and more) argument
  29.     }
  30.    
  31.     else 
  32.     {
  33.         ofstream fout;
  34.         fout.open("counter.txt");
  35.  
  36.         for(i = 2; i < argc; i++)         // for all files
  37.         {
  38.             count = 0;        // reset counter
  39.             index = 0;        // reset index
  40.             fileptr = fopen(argv[i], "r");    // open file with fopen, getc() want file pointers, not streams
  41.        
  42.        
  43.        
  44.             wordlength=strlen(argv[1]);  // argv[1] instead of argv[2]
  45.  
  46.             do 
  47.             {
  48.                 letter = (char) getc(fileptr);    // casted to char
  49.                 for (n=0; n < wordlength; n++)
  50.                 {
  51.                     if (letter==argv[1][n