Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to do it?

Reply
Thread Tools

How to do it?

 
 
seema_coma@yahoo.co.in
Guest
Posts: n/a
 
      10-11-2005
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

 
Reply With Quote
 
 
 
 
sat
Guest
Posts: n/a
 
      10-11-2005
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: oups.com...
> 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
>



 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-11-2005
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.
 
Reply With Quote
 
Jason Curl
Guest
Posts: n/a
 
      10-11-2005
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
>

 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      10-11-2005
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: <private.php?do=newpm&u=>


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-11-2005
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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-11-2005
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) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57