简体   繁体   中英

How do you implement options for your C programs in Unix Terminal?

I want to add a -r option to my c program. So that when it is used in the command line in the terminal it will trigger a different operation for my program. How do I tell my c program that when it sees the text -r in particular that it will jump to the piece of code that will do that alternate functionality?

What I have done so far: So far I have setup my c program to handle files, but not options so my program is able to handle text files in this manner it formats the text files in some way and prints it out stdout:

./p4.out < sometextfile

./p4.out sometextfile

From C by Discovery by Foster I figured out how to do this by using the FILE pointer and the following form of the main program:

void main(argc, char *argv[])

In my code I have if statements to indicate whether or not there is an input file to be processed or just read from standard input via the following if statements:

if(argc == 1){

//do something with standard in (stdin)

}

if(argc == 2){

//do something with one file parameter (sometextfile)

}

The functionality I want to add: In particular my c program should be able to take parameters like this:

./p4.out -r
./p4.out -r sometextfile
./p4.out sometextfile -r

The -r functionality should be read by my main program and then indicate that the characters in the text file should be reversed. C by discovery 2nd edition doesn't mention how to add options to c programs so I don't know what to do.

您可以算出特定程序的逻辑,也可以使用此处所述的库: http : //www.gnu.org/software/libc/manual/html_node/Parsing-Program-Arguments.html#Parsing-Program -Arguments

You can accomplish this by iterating over the arguments provided by the user in the argv array, comparing them to your argument values, and setting flags to indicate that an argument has been used.

int reverse = 0;
const char* file = NULL;
for( int i = 1; i < argc; i++ )
{
    if( strcmp( argv[i], "-r" ) == 0 )
    {
        reverse = 1;
    }
    else
    {
        file = argv[i];
    }
}

if( reverse )
{
    // Reverse the text
}
else
{
    // Do something else
}

Taken from gnu.org

If the syntax for the command line arguments to your program is simple enough , you can simply pick the arguments off from argv by hand . But unless your program takes a fixed number of arguments, or all of the arguments are interpreted in the same way (as file names, for example), you are usually better off using getopt (see Getopt ) or argp_parse (see Argp ) to do the parsing.

if you simple compile the program by using cc command or by gcc then by default executable file name is a.out. If you want to remove that one at the time of compilation you can give any name that you want. And the second thing ./ meaning your current directory. I don't know how to execute without using ./ . May be it help full for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM