2014. 7. 7. 17:30

실행파일 매개변수1 매개변수2 ... 여러개 넣어 실행하게 되면 이것을 출력해 주는 소스

#include <stdio.h>

int main(int argc, char **argv)
{
    int i;

    for (i=0; i < argc; i++)
    {
        printf("argv[%d] = %s\n", i, argv[i]);
    }

    return 0;
}



출력 예

> test0316.exe 1 2 3 "4 5 6"
argv[0] = test0316.exe
argv[1] = 1
argv[2] = 2
argv[3] = 3
argv[4] = 4 5 6




Posted by 해비
2014. 7. 7. 17:29


파일의 내용을 매개변수로 받아서 출력해 준다.

#include <stdio.h>
 
int main(int argc, char **argv)
{
    FILE *pFile = NULL;

    if(argc < 2)
    {
        printf("File Viewer v0.1 [USAGE] testcon1 [FILENAME]\n");
    }
    else
    {
        pFile = fopen(argv[1], "r");
    }

    if( pFile != NULL )
    {
        char strTemp[255];
        char *pStr;
 
        while( !feof( pFile ) )
        {
            pStr = fgets( strTemp, sizeof(strTemp), pFile );
            //printf( "%s", strTemp );
            printf( "%s", pStr );
        }
        fclose( pFile );
    }
    else
    {
        //에러 처리
        printf("Error - can not open file. \n");
    }
 
    return 0;
}


while 문 안에서 eof 가 아닌동안 계속해서 fgets 로 내용 읽어다가 출력을 반복하고 있다.

strTemp 의 크기가 255 이므로 254자(255번재는 마지막을 표시하는 문자) 단위로 읽어서 화면에 뿌려준다.



Posted by 해비