Thread: problem passing an option as command line argument

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    Question problem passing an option as command line argument

    This program reads a file given as command line argument
    and outputs all the lines in the file that contain the
    sequence of characters WORD. WORD is passed in as command line argument. I added an option to this code where the user can chose to have the line number written out as well. The option is indicated by a command line argument -n. But when i use this option it keeps telling me that am passing a wrong parameter.IS there a reason why it doesnt recognise "-n" as a string?



    [code]

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    typedef char * STRNG;

    STRNG code[80];
    STRNG keep[80];
    STRNG get_line(FILE * inf)
    {/*Start of the function which reads one line*/
    STRNG s = NULL;
    int count = 0;
    int tok; // must be int - EOF is an int, not a char

    while ( (tok=getc(inf)) != EOF && tok != '\n' )
    {
    if ( s == NULL ) s = malloc( 81 );
    s[count++] = tok;
    }
    if ( tok != EOF )
    {
    if ( s == NULL ) s = malloc( 81 ); // possibly a newline on its own
    s[count++] = tok; // append the \n
    s[count] = '\0';
    }
    return s;
    }

    int main(int argc, char *argv[])
    {
    char *filename;
    FILE *stream;
    int i=0,rest,count=0, n = 0;
    char *loc;

    char *linenum;
    STRNG word =argv[1];
    filename =argv[2];
    stream=fopen(filename,"r");
    if (argc ==4)
    {
    linenum=argv[1];
    word =argv[2];
    filename=argv[3];
    stream=fopen(filename,"r");

    }

    if (linenum !="-n")
    {printf("Wrong parameter.Try <n>\n");}/*i guess the problem starts here*/

    if(word==NULL)
    {
    printf("Syntax error.Program will exit.\n");
    exit(1);
    }


    if(stream ==NULL)
    {
    printf("Enter a valid filename.Program will exit.\n");
    exit(1);
    }
    while((code[n]=get_line(stream))!=NULL)
    {
    n++;
    }
    i=n;

    for (n=0;n<i;n++)
    {
    count++;
    if(strlen(code[n])>80)
    {
    printf("Error: Line %d is too big\n",++n);
    exit(1);
    }
    loc=strstr(code[n],word);
    if(loc!=NULL){
    if (argc==3)printf("%s",code[n]);
    else
    if ((argc==4)&&(linenum=="-n"))/*same problem here*/
    {
    printf("%d %s", count, code[n]);
    }
    }

    }


    return 0; // 0 is success
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (linenum !="-n")
    Should be
    if ( strcmp(linenum,"-n") != 0 )

    Gotta use strcmp to compare those strings

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    13
    what can i say man.you are the best

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > what can i say man.you are the best

    That's why we pay 'm the big bucks.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Argument passing
    By Tarper67 in forum C++ Programming
    Replies: 1
    Last Post: 06-13-2003, 02:59 PM
  2. weird problem with passing a value..
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 06-21-2002, 09:05 AM
  3. Passing Value back problem
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-10-2002, 05:22 PM
  4. Passing array as argument?
    By evilmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-15-2001, 05:06 AM
  5. Problem in Getting Menu Option
    By seh_hui in forum C Programming
    Replies: 3
    Last Post: 12-06-2001, 08:39 PM