C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 26th, 2003, 04:16 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 6
How to use argument within main(argc, argv)

I got this code from a book. It said "The implementation of waitfile uses fstat to extract the time when the file was last changed".

1) The program has two argument in main as in
main(argc, argv).
Do I included these value at the command line when I
run the program as in ./waitfile prog1 a

2) How do I get this program to run ?

3) How does main(argc, argv) work ?

4) root:~# ./waitfile
./waitfile: Ã3: Unknown error 3221225157


=======================================
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

char *progname;

main(argc, argv)
int argc;
char *argv[];

{
int fd;
struct stat stbuf;
time_t old_time = 0;

progname = argv[0];
if (argc < 2)
error ("Usage: %s filename [cmd]", progname);
if ((fd = open(argv[1], 0)) == -1)
error ("can 't open %s", argv[1]);
fstat(fd, &stbuf);
while (stbuf.st_mtime != old_time)
{
old_time = stbuf.st_mtime;
sleep(10);
fstat(fd, &stbuf);
}

if (argc == 2)
{
execlp("cat", "cat", argv[1], (char *) 0);
error ("can 't execute cat %s", argv[1]);
}
else
{
execvp(argv[2], &argv[2]);
error ("can 't execute %s", argv[2]);
}
exit(0);
}
=========================================

Reply With Quote
  #2  
Old June 26th, 2003, 04:27 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
basically, argc is an integer that contains the number of arguments passed to the program and argv is an array of the arguments that was passed.

argv[0] is always the program name itself.

Reply With Quote
  #3  
Old June 26th, 2003, 04:48 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 6
How to run the program

Please show me how to run this program on a command line.
Is it

./waitfile program-name second-argument

1) What should second-argument contains ?

Reply With Quote
  #4  
Old June 26th, 2003, 04:52 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 23 m 55 sec
Reputation Power: 462
First, your function header is in the oldy-moldy K&R style which does not conform to ANSI C. gcc still compiles it, but you should use the standard instead of an anachronism:

Change:
main(argc, argv)
int argc;
char *argv[];
{

to:
int main(int argc, char *argv[])
{

Quote:

1) The program has two argument in main as in
main(argc, argv).
Do I included these value at the command line when I
run the program as in ./waitfile prog1 a

Those two parameters are the command line.
argv is an array of strings which contain the command and every one of the parameters given on the command line. argc is the number of strings in argv. You use them to read in the parameters.

Quote:

2) How do I get this program to run ?

You have to give it a filename or else it will crash. See 4).

Quote:

3) How does main(argc, argv) work ?

The book must describe it somewhere, because this has been a C standard for just about as long as there's been C.

As I said, argv is an array of strings which contain the command and every one of the parameters given on the command line and arcg is the number of strings in argv. parameters are delimited by white space. So using your example of ./waitfile prog1 a :
argc is the number of parameters plus the command; here it is 3.
argv[0] is the command that was entered. It would be waitfile (I'm not sure if the directory path (./) is also part of the string).
argv[1] is the first parameter, which here is prog1 .
argv[2] is the second parameter, which here is a .

In the code, we see that the first parameter is used as a filename in the fopen() call. We also see that the first parameter is required because if argc is less than 2 then all that was entered in was the command and no parameters. Later, we see that if argc is equal to 2, then only one parameter was entered, so we do not use the command, else a second parameter was entered so we do use a command.

The thing to remember is that the value of argc will always be one more than the number of parameters entered and that parameters 1 through n will be in argv[1] through argv[n].

BTW, execlp and execvp will replace the current program with the program being called, so they never return from the call.

Quote:

4) root:~# ./waitfile
./waitfile: Ã3: Unknown error 3221225157

You didn't give the program any filename, so it exited with an error message. BTW, I have no idea what the code is for the error() function and I could not find it in the man pages. What's up with that function?

Reply With Quote
  #5  
Old June 26th, 2003, 05:02 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 23 m 55 sec
Reputation Power: 462
Re: How to run the program

Quote:
Originally posted by linh
Please show me how to run this program on a command line.
Is it

./waitfile program-name second-argument

1) What should second-argument contains ?

Whatever bash command you want it to be, by the looks of it. It defaults to cat, but you could do rm, mv, cp, less, or whatever you want to. Though I do not understand how that is supposed to relate to the filename in the first parameter.

If the second argument contains white space, then you will need to put quotes around it to keep the shell from interpreting it; eg:
./waitfile foo.bar "mv *.* .."

I'm a bit hazy on execvp(). The first parameter is the name of the file associated with the command and the second is the argv array that you are passing to that command. You should do a web search on execvp() to a better explanation and examples.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to use argument within main(argc, argv)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT