On 07/30/2013 10:58 PM, nilay wrote:
> Hi,
> I want to do the following,
>
> ==========================================
> int main(int argc, char *argv[])
> {
>
> ...
> ...
> if (*argv[1])
> {
> ...
> }
>
> ...
> }
> ==========================================
>
> as i tried to show above, ...
It's a very bad idea to ask how to write code that does something, and
then to explain what you want the code to do by writing code. If you
don't know how to write such code, the code you do write will probably
do something entirely different from what you want to do. You've
provided an excellent example of this.
The code above takes the second element of argv (without first checking
to determine whether there IS a second element - which means your code
has undefined behavior if it is invoked with no arguments), and checks
whether or not the first char of that string is '\0'. That has almost
nothing to do with your stated goal:
> ... I want to pass a filter condition
> at command-line input. And I want to convert that input string
> to 'if-condition expression' so that it can be used in the program.
> How can it be done?? I am struggling very hard to get it right.
All command line inputs are text strings. If those strings describe
filter conditions, you're going to have to decide what kinds of filter
conditions you want to support, and then define a syntax for specifying
those conditions on the command line. Then you'll have to write code
that reads the command line arguments, parsing them according to the
grammar you've specified, and converting them into some internal
representation. Then write code that uses the internal representation to
implement the filter.
That probably sounds to you like an excessively vague,
nearly-meaningless string of jargon, and there's a reason for that.
Until you've specified what kinds of filter conditions you want your
program to support, it's hard to say anything more specific than that
about how to do it.
--
James Kuyper