Linux C newbie question

Discussion in 'C Programming' started by Ed LaBonte, Jul 9, 2004.

  1. Ed LaBonte

    Ed LaBonte Guest

    I'm a rank beginner in C programming and I'm using gcc in a Linux OS. I am
    getting my feet wet with a little program which trains people to calculate
    days of the week from dates in their head. The practice exercizes are in c
    and the tutorial is a text file in the same directory as the executable.
    I access the tutorial from within the program by using the system()
    function with "less". system("less tutorial"). That works fine so long as
    the user is in the right directory when he is running the program. I
    realize that the best way would be to specify the complete path, but I
    don't know where the user will be installing the program directory. Is
    there any way I can specify that it is in the same directory as the
    executable? What would be the best way to handle this problem?
     
    Ed LaBonte, Jul 9, 2004
    #1
    1. Advertising

  2. Ed LaBonte

    Mike Wahler Guest

    "Ed LaBonte" <> wrote in message
    news:p...
    > I'm a rank beginner in C programming and I'm using gcc in a Linux OS. I am
    > getting my feet wet with a little program which trains people to calculate
    > days of the week from dates in their head. The practice exercizes are in c
    > and the tutorial is a text file in the same directory as the executable.
    > I access the tutorial from within the program by using the system()
    > function with "less". system("less tutorial"). That works fine so long as
    > the user is in the right directory when he is running the program. I
    > realize that the best way would be to specify the complete path, but I
    > don't know where the user will be installing the program directory. Is
    > there any way I can specify that it is in the same directory as the
    > executable? What would be the best way to handle this problem?


    On many implementations, 'main()'s second parameter, 'argv's first
    array element, i.e. 'argv[0]' will point to the executable file name
    used to invoke it. This string often includes the 'full path', e.g.
    "C:\MyDir\MyProg.exe". This may or may not be the case on your system.

    Take a look with something like this:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
    printf("Executable name: %s\n", (argc > 0) && (argv[0][0])
    ? argv[0]
    : "[not available]");

    return 0;
    }

    -Mike
     
    Mike Wahler, Jul 9, 2004
    #2
    1. Advertising

  3. Ed LaBonte

    Ed LaBonte Guest

    On Fri, 09 Jul 2004 16:56:16 +0000, Mike Wahler wrote:


    > On many implementations, 'main()'s second parameter, 'argv's first
    > array element, i.e. 'argv[0]' will point to the executable file name
    > used to invoke it. This string often includes the 'full path', e.g.
    > "C:\MyDir\MyProg.exe". This may or may not be the case on your system.
    >
    > Take a look with something like this:
    >
    > #include <stdio.h>
    >
    > int main(int argc, char *argv[])
    > {
    > printf("Executable name: %s\n", (argc > 0) && (argv[0][0])
    > ? argv[0]
    > : "[not available]");
    >
    > return 0;
    > }
    >
    > -Mike


    No, but thanks for the response. argv[0] just returns whatever was typed
    in. If the program is in the path and you just type "programname",
    argv[0] is just "programname". If you type in the whole path then that
    will be in argv[0] also, but only if you enter the whole path.

    But thanks again for the response.
     
    Ed LaBonte, Jul 9, 2004
    #3
  4. On Fri, 9 Jul 2004, Ed LaBonte wrote:

    > I'm a rank beginner in C programming and I'm using gcc in a Linux OS. I am
    > getting my feet wet with a little program which trains people to calculate
    > days of the week from dates in their head. The practice exercizes are in c
    > and the tutorial is a text file in the same directory as the executable.
    > I access the tutorial from within the program by using the system()
    > function with "less". system("less tutorial"). That works fine so long as
    > the user is in the right directory when he is running the program. I
    > realize that the best way would be to specify the complete path, but I
    > don't know where the user will be installing the program directory. Is
    > there any way I can specify that it is in the same directory as the
    > executable? What would be the best way to handle this problem?


    If something isn't working, try something different.

    In other words, don't use the system() command. The system() command is
    not very flexible and gives you little options when something has gone
    wrong.

    Instead, consider learning to open a file using fopen(), reading the
    contents in using fgets() and then printing it to the screen using puts().
    This way, when you attempt to open the file and it is not in the current
    directory you can print a helpful message to the user. For example,

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

    int main(void)
    {
    const char filename[] = "tutorial";
    FILE *fp;

    fp = fopen(filename, "r");
    if(fp == NULL) {
    fprintf(stderr, "Open '%s' for read failed.\n", filename);
    exit(EXIT_FAILURE);
    }

    /* do the rest here */

    fclose(fp);
    return 0;
    }

    Another option is to learn about using command line arguments. You could
    then have it so the user has to specific the location of the tutorial file
    when they run the program.

    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
     
    Darrell Grainger, Jul 10, 2004
    #4
  5. Ed LaBonte

    Gwar Guest

    On Fri, 10 Jul 2004, Darrell Grainger wrote:

    > On Fri, 9 Jul 2004, Ed LaBonte wrote:
    >
    > > I'm a rank beginner in C programming and I'm using gcc in a Linux OS. I am
    > > getting my feet wet with a little program which trains people to calculate
    > > days of the week from dates in their head. The practice exercizes are in c
    > > and the tutorial is a text file in the same directory as the executable.
    > > I access the tutorial from within the program by using the system()
    > > function with "less". system("less tutorial"). That works fine so long as
    > > the user is in the right directory when he is running the program. I
    > > realize that the best way would be to specify the complete path, but I
    > > don't know where the user will be installing the program directory. Is
    > > there any way I can specify that it is in the same directory as the
    > > executable? What would be the best way to handle this problem?

    >
    > If something isn't working, try something different.
    >
    > In other words, don't use the system() command. The system() command is
    > not very flexible and gives you little options when something has gone
    > wrong.



    I don't necessarily disagree with the approach this poster is bringing up,
    but as an alternative to system, you might want to check out execl,
    execle, execlp, execv, execve, or execvp functions, declared in unistd.h.




    >
    > Instead, consider learning to open a file using fopen(), reading the
    > contents in using fgets() and then printing it to the screen using puts().
    > This way, when you attempt to open the file and it is not in the current
    > directory you can print a helpful message to the user. For example,
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > const char filename[] = "tutorial";
    > FILE *fp;
    >
    > fp = fopen(filename, "r");
    > if(fp == NULL) {
    > fprintf(stderr, "Open '%s' for read failed.\n", filename);
    > exit(EXIT_FAILURE);
    > }
    >
    > /* do the rest here */
    >
    > fclose(fp);
    > return 0;
    > }
    >
    > Another option is to learn about using command line arguments. You could
    > then have it so the user has to specific the location of the tutorial file
    > when they run the program.
    >
    > --
    > Send e-mail to: darrell at cs dot toronto dot edu
    > Don't send e-mail to
    >
     
    Gwar, Jul 10, 2004
    #5
  6. Ed LaBonte

    Dan P. Guest

    "Darrell Grainger" <> wrote in message
    news:p...
    > On Fri, 9 Jul 2004, Ed LaBonte wrote:
    >
    > > I'm a rank beginner in C programming and I'm using gcc in a Linux OS. I

    am
    > > getting my feet wet with a little program which trains people to

    calculate
    > > days of the week from dates in their head. The practice exercizes are in

    c
    > > and the tutorial is a text file in the same directory as the executable.
    > > I access the tutorial from within the program by using the system()
    > > function with "less". system("less tutorial"). That works fine so long

    as
    > > the user is in the right directory when he is running the program. I
    > > realize that the best way would be to specify the complete path, but I
    > > don't know where the user will be installing the program directory. Is
    > > there any way I can specify that it is in the same directory as the
    > > executable? What would be the best way to handle this problem?

    >
    > If something isn't working, try something different.
    >
    > In other words, don't use the system() command. The system() command is
    > not very flexible and gives you little options when something has gone
    > wrong.
    >
    > Instead, consider learning to open a file using fopen(), reading the
    > contents in using fgets() and then printing it to the screen using puts().
    > This way, when you attempt to open the file and it is not in the current
    > directory you can print a helpful message to the user. For example,
    >


    <Code snipped>

    >Another option is to learn about using command line arguments. You could
    >hen have it so the user has to specific the location of the tutorial file
    >when they run the program.



    In addition to your advice, the OP probably needs to check out a Linux
    newsgroup, or better yet just search Google, to find the Linux command which
    returns the application's path. Of course, that solution is not portable,
    but I think it's a little nicer than asking the user of the program to type
    in the full path of the file everytime they run the program.

    Another option is to set up a configuration file where one of the parameters
    is the full path to the file. That way, they can type it in just once in
    that file. Then if it can't find it there, it can exit out gracefully and
    display an error message like you had in your program.



    Dan
     
    Dan P., Jul 10, 2004
    #6
  7. Ed LaBonte

    Dan Pop Guest

    In <> (Darrell Grainger) writes:

    >In other words, don't use the system() command. The system() command is
    >not very flexible and gives you little options when something has gone
    >wrong.


    What is this mythical "system() command" you're talking about?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jul 12, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. U. George
    Replies:
    4
    Views:
    851
    U. George
    Aug 11, 2005
  2. John Ryan
    Replies:
    3
    Views:
    383
    Troll_King
    Nov 2, 2003
  3. Ralph Moritz
    Replies:
    1
    Views:
    563
    Rhino
    Jul 6, 2006
  4. Jerry C.
    Replies:
    8
    Views:
    256
    Uri Guttman
    Nov 23, 2003
  5. moonhkt
    Replies:
    2
    Views:
    223
    Chris Angelico
    Sep 16, 2012
Loading...

Share This Page