프로그램 실행명령어 뒤에 옵션 주는 법........

익명 사용자의 이미지

안녕하세요......숙제를 하고 있는데요......
어떤 프로그램을 만들어서 a라는 실행파일을 만드는데......
그 녀석을 실행할 때.......다른 리눅스 명령들처럼......
뒤에 옵션을 줄 때는 어떻게 해야되나요....
예를 들어서......
./a [filename] -u [username]과 같은 식으로 명령을 주면
뒤에 나오는 파일명과 사용자 이름에 대해서 어떤 작업을 수행하게 되는
프로그램인데요......
C프로그래밍을 할 때 어떻게 해야 될까요?????
찾아봐도 못찾겠구......도와주세요.

익명 사용자의 이미지

초보회원님... 이문제는 기초 C책에 자세히 나와있습니다.

C를 공부하신다면 C기초문법책에 자세히 나와있습니다..

어떻게 이용하는 것인지만 설명해드리겠습니다..

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

위 main함수를 보시면 argc와 char * []의 argv가 있습니다..

argc는 님이 말씀하신것과 같이 ./a 뒤에오는 인자의 수를 나타냅니다..
님이 주신 arguments는 3개 이므로 main()함수로 넘어올때 argv 값은 4가
됩니다..

argv[]는 뒤에오는 인자의 문자열을 가지고 있습니다..

-----------------------------------------------------------------
다음 소스를 보시기 바랍니다.

int an;
char *arg =0;

for (an=1; an 아시겠죠... ./a 실행파일 다음부터 인자가 시작되니 1부터 시작하는 것이
지요..
arg = argv[an]; // 요기서 만약 님이 ./a -u filename 이렇게 인자
를 주셨다면..
if (*arg == '-') { // 요기서 *arg의 값은 '-'가됩니다..
pfilename = 0;

switch (*(arg+1)) { // 요기서 'u'를 읽는 것이지요..

case 'u'
************************************************************
이 부분에서 -u 다음 인자를 filename을 읽어 처리하시면 되는겁니다..
이렇게 프로그래밍 하시는 것입니다.. 그럼 즐프 하세여.
************************************************************
break;
}
}
}

익명 사용자의 이미지

#include
#include

int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;

opterr = 0;

while ((c = getopt (argc, argv, "abc ")) != -1) {
switch (c)
{
case 'a'
aflag = 1;
break;
case 'b'
bflag = 1;
break;
case 'c'
cvalue = optarg;
break;
case '?'
if (isprint (optopt))
fprintf(stderr, "Unknown option `-%c'. \n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'. \n", optopt);
return 1;
default
abort ();
}
}
printf("aflag = %d, bflag = %d, cvalue = %s\n", aflag, bflag,
cvalue);

for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv

    );
    return 0;
    }

    다음은 위의 프로그램을 여러 가지 인수들의 조합을 사용했을 때 어떤 결
    과를 나타내는지에 대한 예이다.

    % testopt
    aflag = 0, bflag = 0, cvalue = (null)

    % testopt -a -b
    aflag = 1, bflag = 1, cvalue = (null)

    % testopt -ab
    aflag = 1, bflag = 1, cvalue = (null)

    % testopt -c foo
    aflag = 0, bflag = 0, cvalue = foo

    % testopt -cfoo
    aflag = 0, bflag = 0, cvalue = foo

    % testopt arg1
    aflag = 0, bflag = 0, cvalue = (null)
    Non-option argument arg1

    % testopt -a arg1
    aflag = 1, bflag = 0, cvalue = (null)
    Non-option argument arg1

    % testopt -c foo arg1
    aflag = 0, bflag = 0, cvalue = foo
    Non-option argument arg1

    % testopt -a -- -b
    aflag = 1, bflag = 0, cvalue = (null)
    Non-option argument -b

    % testopt -a -
    aflag = 1, bflag = 0, cvalue = (null)
    Non-option argument -

    더 자세한 정보
    http//database.sarang.net/study/glibc/22.htm#1

    익명 사용자의 이미지

    굉장히 좋은 소스인것 같습니다..

    제가 쓴 소스는 노가다인 반면 이 함수를 사용하면 많은 코딩을 줄일수 있
    군여..^^..

    좋은 공부가 되었습니다.

    익명 사용자의 이미지

    댓글 달기

    Filtered HTML

    • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
    • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
    • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
    • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

    BBCode

    • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
    • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
    • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
    • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

    Textile

    • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
    • You can use Textile markup to format text.
    • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

    Markdown

    • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
    • Quick Tips:
      • Two or more spaces at a line's end = Line break
      • Double returns = Paragraph
      • *Single asterisks* or _single underscores_ = Emphasis
      • **Double** or __double__ = Strong
      • This is [a link](http://the.link.example.com "The optional title text")
      For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
    • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
    • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

    Plain text

    • HTML 태그를 사용할 수 없습니다.
    • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
    • 줄과 단락은 자동으로 분리됩니다.
    댓글 첨부 파일
    이 댓글에 이미지나 파일을 업로드 합니다.
    파일 크기는 8 MB보다 작아야 합니다.
    허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
    CAPTCHA
    이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.