How to do it?

Discussion in 'C Programming' started by seema_coma@yahoo.co.in, Oct 11, 2005.

  1. Guest

    Hi all,
    I am new to C programming and unix, and I want to write something
    similar to this

    if exists ~seema/TimerTest
    then execute ~seema/TimerTest;

    Can some body explain how to write this in C.
    Where ~seema/TimerTest is another executable

    Thanks in Advance,
    Seema Rao
    , Oct 11, 2005
    #1
    1. Advertising

  2. sat Guest

    chck this
    ////////////////////////////////////////
    FILE fp;
    char* filename="~seems/Timer";


    fp = fopen (filename,"r"); // try to open the file first in read mode
    if ( fp == NULL )
    {
    printf ("\nFile doesnt exist");
    return;
    }
    // file exists
    close(fp);

    system( filename); //executes a command in a new shell
    ////////////////////////////////////////
    hope you know where to include this code ( include it in the main() )


    this should do the trick for what you have asked.
    But if you really want to have some control on what you are executing, then
    you have to probably go for some Exec() functions..




    <> wrote in message
    news:...
    > Hi all,
    > I am new to C programming and unix, and I want to write something
    > similar to this
    >
    > if exists ~seema/TimerTest
    > then execute ~seema/TimerTest;
    >
    > Can some body explain how to write this in C.
    > Where ~seema/TimerTest is another executable
    >
    > Thanks in Advance,
    > Seema Rao
    >
    sat, Oct 11, 2005
    #2
    1. Advertising

  3. wrote:

    > I am new to C programming and unix, and I want to write something
    > similar to this


    > if exists ~seema/TimerTest
    > then execute ~seema/TimerTest;


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

    int main( void )
    {
    FILE *fp;

    if( (fp=fopen("~seema/TimerTest","r")) != NULL ) {
    fclose( fp );
    system( "~seema/TimerTest" );
    }
    return 0;
    }

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Oct 11, 2005
    #3
  4. Jason Curl Guest

    wrote:
    > Hi all,
    > I am new to C programming and unix, and I want to write something
    > similar to this
    >
    > if exists ~seema/TimerTest
    > then execute ~seema/TimerTest;
    >
    > Can some body explain how to write this in C.
    > Where ~seema/TimerTest is another executable


    You may also want to ask comp.unix.programmer. They could help you
    better. The C language itself doesn't go into much detail about
    executing binaries (and so a solution that is portable C may not be
    precisely what you want), while Unix implementations expand on this
    quite a bit.

    >
    > Thanks in Advance,
    > Seema Rao
    >
    Jason Curl, Oct 11, 2005
    #4
  5. Christopher Benson-Manica wrote:
    >
    > wrote:
    >
    > > I am new to C programming and unix, and I want to write something
    > > similar to this

    >
    > > if exists ~seema/TimerTest
    > > then execute ~seema/TimerTest;

    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main( void )
    > {
    > FILE *fp;
    >
    > if( (fp=fopen("~seema/TimerTest","r")) != NULL ) {
    > fclose( fp );
    > system( "~seema/TimerTest" );
    > }
    > return 0;
    > }


    Failure to open a file for reading does not necessarily mean that the
    file is not executable. Under Unix, for example, you can have execute-
    only permissions (---x--x--x) on a program. Also, being able to open
    a file for reading doesn't mean it's executable.

    You probably need to go outside the realm of standard C, and use an
    extension provided by the platform. For example, on Posix systems,
    you can try access() or stat() to see if the file not only exists, but
    that you can execute it.

    And, I am also assuming that "~seema" is meant to be treated as the
    Unix "seema's home directory" shortcut. This obviously requires more
    system-specific extensions, such as getpwnam().

    Perhaps comp.unix.programmer is a better place to find answers?

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Oct 11, 2005
    #5
  6. Kenneth Brody <> wrote:

    > Perhaps comp.unix.programmer is a better place to find answers?


    I'm certain that it is, but the topical solution at least has a chance
    of working correctly.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Oct 11, 2005
    #6
  7. writes:
    > I am new to C programming and unix, and I want to write something
    > similar to this
    >
    > if exists ~seema/TimerTest
    > then execute ~seema/TimerTest;
    >
    > Can some body explain how to write this in C.
    > Where ~seema/TimerTest is another executable


    You might consider just trying to execute it (using system(), and
    handling the '~' somehow). If the system() call succeeds, the
    executable must have existed; if it fails, *something* went wrong.

    The result returned by system() is implementation-specific; you might
    not be able to distinguish between the executable not existing and the
    program failing for some other reason.

    On some systems, the relationship between executable files and the
    string you pass to system() is not simple. For example, you might
    invoke system("foo") to execute "foo.exe"

    The best solution is likely to be implementation-specific (depending
    on how much you care about portability).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 11, 2005
    #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.

Share This Page