LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2008, 08:19 AM   #1
njac
Member
 
Registered: Feb 2007
Posts: 38

Rep: Reputation: 15
How can I compile program to use command line arguments


How can I compile program to use command line arguments?
for example:

./a.out -filename

Compiler is g77.
thanks!
 
Old 10-02-2008, 09:17 AM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
I don't know how it works in fortran but, as it's service provided by the OS, similar mechanism must exist. In C, the entry point is always the function called "main", and the system give to this function two parameters: the first one is an integer and the second one is an array of strings, the number of strings in the array is given by the first parameter. The function prototype is:
Code:
int main (int argc, char* argv[]);
The first string of the second parameter is the program name typed in the command line. Then if you type "./a.out -t foo -v bar", then the system will pass to your main function:
argc = 5
argv[0] = "./a.out"
argv[1] = "-t"
argv[2] = "foo"
argv[3] = "-v"
argv[4] = "bar"
The standard C library provide an helper to ease the arguments handling, its name is "getopt", "man 3 getopt" will give you all the details you need.
 
Old 10-02-2008, 09:34 AM   #3
njac
Member
 
Registered: Feb 2007
Posts: 38

Original Poster
Rep: Reputation: 15
Thank you for your answer!
Well, now I have basic idea how it works, but fortran is quite different story. Can someone tell me more??
 
Old 10-02-2008, 10:36 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

http://gcc.gnu.org/onlinedocs/gcc-3.3.6/g77/

Look for "GetArg" and "IArgC" intrinsics.
 
Old 10-03-2008, 03:50 AM   #5
njac
Member
 
Registered: Feb 2007
Posts: 38

Original Poster
Rep: Reputation: 15
This is realy simple, just call:
call getarg(n, arg)
,where:

integer n -is number of argument
character (len= ..) arg -is n-th argument

For example, this:

character (len=10) arg
call getarg(2, arg)
print*, arg
end


with this:

./a.out aaa bbb

gives output:

bbb

Thank you for your help.

Last edited by njac; 10-03-2008 at 03:52 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need some help regarding command line arguments kristam269 Linux - General 1 01-23-2007 09:40 AM
command line arguments nickraj Programming 6 09-11-2006 01:01 PM
Command line arguments?? almagerenia Linux - Newbie 1 09-08-2006 04:05 AM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 08:43 AM
Command line arguments to java program Majjj Programming 3 10-23-2003 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration