如果我的程序有两个命令行参数,就像这样

./program hey.txt hello


但我想接受这样的第一个论点

./program hello < hey.txt


我该怎么做?

最佳答案

main( int argc, const char* argv[] )
{
    FILE* inputFile = NULL;
    if ( argc == 3 )
    {
       // I have 2 arguments. The first is a file name, the second is my other argument.
       inputFile = fopen( argv[1], "r" );
    } else if ( argc == 2 ) {
       // i have one argument so the input will come from stdin.
       inputFile = stdin;
    }

    // now read the file somehow.... like with fread.
    fread( inputFile );
}

关于c - c-如何通过重定向接受命令行参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26070346/

10-11 23:10
查看更多