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, 07: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, 07: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,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
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: 592

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

Reply With Quote
  #3  
Old October 4th, 2005, 08: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, 08: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,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
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, 03: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, 03: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,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
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, 03:54 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
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, 03:57 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
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 05:47 AM. Reason: Now the example makes sense :)

Reply With Quote
  #9  
Old October 5th, 2005, 04: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,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
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]) 
  52.                     {
  53.                         index++;
  54.                         letter = (char) getc(fileptr);    // casted to char
  55.                     }
  56.                     else 
  57.                     {
  58.                         index=0;
  59.                     }
  60.                 }
  61.            
  62.                 if(index==wordlength) 
  63.                 {
  64.                     count++;
  65.                     index=0;
  66.                 }
  67.             }
  68.             while(letter!= EOF);
  69.    
  70.             fout << "FILE: " << argv[i] << endl;
  71.             fout << "SEARCHKEY: " << argv[1] << endl;
  72.             fout << "OCCURENCES: " << count << endl;
  73.         }
  74.     }
  75.  
  76.     return 0;
  77. }


With this version, you can supply any number of files after the first one, it'll search them all, and output all info to the counter.txt file

Reply With Quote
  #10  
Old October 5th, 2005, 04:21 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
Quote:
clrscr() requires the <conio.h> library.


weird...I included that particular library already

#include <conio.h>

but the problem still persists.

Edit: Whoops, posted this after you posted your recommendation

Reply With Quote
  #11  
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
Good News: Compilation complete!

Bad News: Microsoft Error Report! It was working fine when I typed in the less than 3 arguments (ie the error message came out properly). But once the proper arguments were put into place, M.E.R!

The file in the file to be opened is called mytext.txt. Inside was a sentence, "The big brown fox jumped over the lazy dog." When it tried to find "dog" in mytext.txt, the error message popped out.

parameter was C:\Finder\finder.exe dog mytext.txt

*Edit: Could it be because I forgot to close the files?
**Edit: Added fout.close(). Didnt solve the problem

Reply With Quote
  #12  
Old October 5th, 2005, 05:00 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,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Hmm, I was bored anyway and was writing this niftier version of the program:
cpp Code:
Original - cpp Code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     // declarations
  8.     int i, keyword, firstfile, n = 0;
  9.     int wordlength = 0;
  10.     int count, index;
  11.     char letter;
  12.     FILE *fileptr, *fout;
  13.  
  14.     fout = stdout;
  15.  
  16.     for(i = 1; i < argc; i++)
  17.     {
  18.         if(argv[i][0] == '-')         // is it a switch?
  19.         {
  20.             switch(argv[i][1])    // switch to decide which switch
  21.             {
  22.                 case 'o':
  23.                     fclose(fout);
  24.                     if(++i < argc)    // make sure the next parameter exists
  25.                         fout = fopen(argv[i], "w");
  26.                     break;
  27.                 case '?':
  28.                 default:
  29.                     fprintf(fout, "Usage: %s [<switches>] <keyword> <filename> [<filename>...]\n", argv[0]);
  30.                     fprintf(fout, "Switches:\n");
  31.                     fprintf(fout, "-o <filename> : Output to file <filename> (default is screen)\n");
  32.                     fprintf(fout, "-? : Output help\n\n");
  33.                     return 0;
  34.             }
  35.         }
  36.         else                    // else it is the keyword!
  37.         {
  38.             keyword = i++;      // set the location of the keyword
  39.             firstfile = i;    // set the location of the first file
  40.             break;        // exit for loop
  41.         }
  42.     }
  43.                
  44.  
  45.     if (i >= argc)          // wrong argument count
  46.     {
  47.         // output quick help
  48.         fprintf(stderr, "Error! Unexpected number of arguments.\n");
  49.         fprintf(stderr, "Usage: %s [<switches>] <keyword> <filename> [<filename>...]\n", argv[0]);
  50.         fprintf(stderr, "Type %s -? for more info\n\n", argv[0]);
  51.     }
  52.     else 
  53.     {
  54.         // start output
  55.         fprintf(fout, "SEARCHKEY: %s\n\n", argv[keyword]);
  56.  
  57.         for(i = firstfile; i < argc; i++)         // for all files
  58.         {
  59.             count = 0;        // reset counter
  60.             index = 0;        // reset index
  61.             fileptr = fopen(argv[i], "r");    // open file with fopen, getc() want file pointers, not streams
  62.        
  63.             wordlength = strlen(argv[keyword]);  // get length of keyword
  64.  
  65.             do 
  66.             {
  67.                 letter = (char) getc(fileptr)
  68.                 for (n=0; n < wordlength; n++)
  69.                 {
  70.                     if (letter==argv[keyword][n]) 
  71.                     {
  72.                         index++;
  73.                         letter = (char) getc(fileptr);   
  74.                     }
  75.                     else 
  76.                     {
  77.                         index=0;
  78.                     }
  79.                 }
  80.            
  81.                 if(index == wordlength) 
  82.                 {
  83.                     count++;
  84.                     index=0;
  85.                 }
  86.             }
  87.             while(letter!= EOF);
  88.    
  89.             // output info for current file
  90.             fprintf(fout, "FILE: %s\n", argv[i]);
  91.             fprintf(fout, "OCCURENCES: %d\n\n", count);
  92.  
  93.             // close current file
  94.             fclose(fileptr);
  95.         }
  96.     }
  97.  
  98.     // if an output file was specified, close it
  99.     if(fout != stdout)
  100.         fclose(fout);
  101.  
  102.     // exit
  103.     return 0;
  104. }


See if this one works.
If it does, that'll teach you not to use streams... old fashioned C is still better...

and see if you can spot what I 'improved'
Comments on this post
B-Con agrees: Old fashioned C rocks

Last edited by Itsacon : October 5th, 2005 at 07:31 AM. Reason: stderr, and ability to make helpfile :D

Reply With Quote
  #13  
Old October 5th, 2005, 05:14 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
Yeah I have to agree, C coding can get much simpler than C++

too bad windows doesnt agree. I wonder if the problems with my pc, and not the program itself.

Reply With Quote
  #14  
Old October 5th, 2005, 05:15 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,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Still not working?
What compiler are you using?
Comments on this post
Icon agrees: Yellow belt! *humble bow*

Reply With Quote
  #15  
Old October 5th, 2005, 05:18 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
Hmm. What compiler are you using? Can you post the exact code you are using? I tried the code you first posted and substituted the letter=getc(fileptr) by letter = fileptr.get() and it worked fine.. (Next to some changes with arv[1-->2] and stuff)..

Reply With Quote
  #16  
Old October 5th, 2005, 05:25 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,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Icon, what compiler?
Can you run my code on one of your windows boxes? (only GCC/G++ here I'm afraid)

PS: And it's only a yellow tip
yellow belt will take another 250 spam^H^H^H^Hposts

Reply With Quote
  #17  
Old October 5th, 2005, 05:27 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
Im using a Dev C++, the compiler downloaded from bloodshed.net

I followed Icon's suggestion, and the Error Report Disapperard. Problem now is that it doesnt want to generate the counter.txt file I want it to make.

Reply With Quote
  #18  
Old October 5th, 2005, 05:30 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,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Strange... that uses the mingw compiler, which is based on gcc IIRC.

Like I said, compiled and ran my code without warnings on gcc/linux

Reply With Quote
  #19  
Old October 5th, 2005, 05:33 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
I followed Icon's suggestion (used the first code I made, edited the stuff he suggested, etc), and the Error Report Disappeared. Problem now is that it doesnt want to generate the counter.txt file I want it to make.

because its the old file, command is C:\Finder\finder2.exe mytext.txt dog

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;
  

   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 = fileptr.get(); //Icon's suggestion
        for (n=0; n < wordlength; n++) 
        {
            if (letter==argv[2][n])  
            {
	        index++;
	         letter = fileptr.get(); //Icon's suggestion
             }
             
	        else  
            {
             index=0;
            }
         }
         
	     if(index==wordlength)  
         {
         count++;
	     index=0;
	     }

      } 
      while(letter!= EOF);
   
//Corrected courtesy of Istacon's observation
   fout << "FILE:" << argv[1] << endl;           
   fout << "SEARCHKEY:" << argv[2] << endl;
   fout << "OCCURENCES" << count << endl;
   
   fout.close(); //I added this little bit 
}

return 0;
}

Reply With Quote
  #20  
Old October 5th, 2005, 05:46 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
First of all if you have this order of arguments you should call:
c:\finder\finder.exe mytext.txt dog instead of the other way around

I suggest to add 'int' in front of main and to put the global index and count variable inside the main. You'll have to remove the extra count declaration. fout.close; --> fout.close(). It works for me, but I am not on windows

You'll also have to replace the while(letter!=EOF) with while( ! fileptr.eof() ).
My previous example did not make the correct point, instead of letter=fileptr.get() you can use fileptr >> letter (if letter is a char) !

If I kept the while(letter!=EOF) then my program was hanging, so I guess that will trigger a microsoft error on windows

Reply With Quote
  #21  
Old October 5th, 2005, 06:02 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
Wait, what extra count declaration?

Got the program to run. Problem now is that it hasnt "stopped"
Something like this:

C:\Documents and Settings\Admins Name>c:\finder\finder2.exe mytext.txt dog

(then it doesnt want to go back to C:\Documents and Settings\Admins Name> anymore)

From the looks of it, the loop doesnt stop.

@Itsacon: Also found the bugs that caused error reports. For some reason, the computer doesnt "like" either int i or argv[i].
They probably caused some sort of conflict between C and C++ functions.

Reply With Quote
  #22  
Old October 5th, 2005, 06:16 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
Ok too many people posting simultaneously, this is getting confusing.. If you move the global int count=0 declaration into the main routine than you will have one too many int count declarations, but that's not important. To get rid of the not stopping problem you need to replace while(letter!=EOF) with while(!fileptr.eof()). This works for me with g++ but should work for dev c++ too I guess. The not stopping problem is also causing counter.txt not to be written of course.
If you want I can post my exact code..

Reply With Quote
  #23  
Old October 5th, 2005, 06:22 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
Added the changes. It should be more identical to your code now. The stopping problem was not solved unfortunately

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

using namespace std;

int main(int argc, char *argv[])
{
   int n=0;
   int wordlength=0;
   int index=0;     /*   transferred count in int main()*/
   int count=0;     /*  did it with index too, for uniformity*/
   char letter;
   FILE *fileptr;
  

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

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

      } 
     while(!fileptr.eof()); //changed this, but it still had 
                                //suffers from the same problem

   fout << "FILE:" << argv[2] << endl;
   fout << "SEARCHKEY:" << argv[1] << endl;
   fout << "OCCURENCES" << count << endl;
   
   fout.close();
}

return 0;
}

Reply With Quote
  #24  
Old October 5th, 2005, 06: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,029 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: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
I say again, try ditching the entire stream business...

Reply With Quote
  #25  
Old October 5th, 2005, 07:37 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
Your code works now for me if I call it as such:
<exe> dog mytext.txt ( this is correct )
when I call it as such:
<exe> mytext.txt dog
then it does not stop.. I don't know what happens when you try to open a non-existing file with ifstream. There must be some nice way of getting that error when you check the link to cplusplus reference site i posted earlier.

Let us know if that was what was going wrong, i.e., wrong order of arguments.

Reply With Quote
  #26  
Old October 5th, 2005, 07:43 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
A quick look at that website (http://www.cplusplus.com/ref/iostream/ifstream/) shows that with the functions is_open() and good() you should be able to check if the fle was opened correctly..
If you change while(!fileptr.eof()) to while(fileptr.good()) it also seems to not to hang, although you want to have an error message nforming you the fle (dog in this case) does not exist..

Of course sticking to the good old C way of doing things is also an option! Itsacon showed that already. I'm guessing you want to learn C++ though, or have to do it for some course you're taking

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > input/output questions and keyword searches


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap