#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Now working program


    hello, i'm not familiar with the programming, but i'm must become beacause i have to go in for an examination :(
    Here's my problem: this program is written in my textbook, but the only result i receive is this message: "Usage: find <filename> <ch>". Is this all that must be displayed?

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

    int main(int argc,char *argv[])
    {
    FILE *fp;
    char ch;

    if(argc!=3){
    printf("Usage: find <filename> <ch>\n");
    exit(1);
    }
    if((fp=fopen(argv[1],"r"))==NULL){
    printf("Cannot open file.\n");
    exit(1);
    }

    while((ch=fgetc(fp))!=EOF)
    if(ch==*argv[2]){
    printf("%c found",ch);
    break;
    }
    fclose(fp);

    return 0;
    }
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by q1cet0o
    Is this all that must be displayed?
    No. If you think that you are doomed as far as any examination is concerned. Most examinations occur at the end of a course of lessons - how come you have got to the examination without the lessons!?

    Read the code, or just follow the instructions. The message output is telling you how to run the code. It is obviously asking you to provide a filename and a character as command line arguments. That is not a programming issue, that is a fundamental aspect of using a computer.

    If you are running the code from the command line, you need to provide two arguments, e.g.:

    find somefile.txt x

    then if somefile.txt exists and contains a 'x' character, a "found" message will be output. If somefile.txt does not exist, the "cannot open" message will be output.

    If you are running the code from some IDE, it will probably have a place in the project or debug configuration where you can add test arguments.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0
    Note your Argument Count (argc) also takes in the count of your Program File Name.

    Ex: if the file in which this program is written is named Program.c.
    Your Command line should look like
    "Program.c filename.txt x" here your argc is 3.

    In simple argc also takes in the profram file name as its input!!

    Hope this helps you to determine the output.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by pratster
    Ex: if the file in which this program is written is named Program.c.
    Your Command line should look like
    "Program.c filename.txt x" here your argc is 3.
    C is not a scripting or interpreted language - you cannot run a .c file! An executable generated from a sourcefile called Program.c can have any name, of which Program.c is most unlikely!

    It is clearly implicit in the question the executable name in this case is "find" so this is just an inaccurate and unnecessary duplicate of my answer.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0
    Oops! Yeah i didn't notice that "find" must be an executable on the cmd interface!

    Misinterpreted it to be the argument(filename)!

    Thanks for letting me know.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    tnx a lot :)

IMN logo majestic logo threadwatch logo seochat tools logo