How to do it?

S

seema_coma

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
 
S

sat

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..
 
C

Christopher Benson-Manica

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;
}
 
J

Jason Curl

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.
 
K

Kenneth Brody

Christopher said:
#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:[email protected]>
 
C

Christopher Benson-Manica

Kenneth Brody said:
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.
 
K

Keith Thompson

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top