| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||||
|
|||||
|
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:
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]; 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. |
|
#3
|
|||
|
|||
|
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++ |
|
#4
|
||||
|
||||
|
|
|
#5
|
|||
|
|||
|
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;
}
|
|
#6
|
||||
|
||||
|
Quote:
|
|
#7
|
||||
|
||||
|
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/ |
|
#8
|
||||
|
||||
|
Stream operator does work (which is also a method of ifstream, an overloaded operator in c++ lingo.. method is java lingo sorry..)
Example: Last edited by Icon : October 5th, 2005 at 06:47 AM. Reason: Now the example makes sense :) |
|
#9
|
||||
|
||||
|
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:
|