C,使用argv []作为命令行参数

时间:2016-09-25 00:18:12

标签: c file argv

我遇到了这个程序的问题,当我使用stdin时它工作得很好但是当我修改它以从命令行获取字符时它不会。我知道我做错了什么但不知道是什么,任何帮助都会非常感激。

描述和代码:

/* Program prints the date in this form: September 13, 2010
    allow the user to  enter date in either 9-13-2010 or 9/13/2010  
    format, otherwise print 'error'  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *month(int m)
{
    char *months[]={"January","February","March","April","May",
                "June", "July","August","September","October",
                "November","December"};
    return months[m-1];
}

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

    int m=0,d=0,y=0;

    FILE *fp;

    if((fp=fopen(argv[1],"rb")) == NULL)
    {
        fprintf(stderr,"Couldn't open the file. ");
        exit(EXIT_FAILURE);
    }


    printf("Type a date (mm-dd-yyyy) or (mm/dd/yyyy): \n");

    if(fscanf(fp,"%d%*[/-]%d%*[/-]%d",&m,&d,&y) != 3)  //store characters in variables
        {
            fprintf(stderr, "Not properly formatted.");
            exit(EXIT_FAILURE);
        }

    printf("%s %2d, %4d",month(m),d,y);

    return 0;
}

输入:

01/30/1990

输出:

Couldn't open the file.

2 个答案:

答案 0 :(得分:2)

我修改了你的程序以解决你遇到的问题(并修复了一些未定义的行为,或者#34; UB&#34;简而言之),但仅限于:

#include <stdio.h>
#include <stdlib.h>

const char *month(int m) {
    const char const *months[] = {
        "January", "February", "March", "April",
        "May", "June", "July", "August",
        "September", "October", "November", "December",
    };

    if (1 <= m && m <= 12) {
        return months[m - 1];
    } else {
        return NULL;
    }
}

int main(int argc, char *argv[]) {
    int m = 0, d = 0, y = 0;

    if (argc == 2) {
        if (sscanf(argv[1], "%d%*[/-]%d%*[/-]%d", &m, &d, &y) != 3) {
            fprintf(stderr, "Not properly formatted.");
            exit(EXIT_FAILURE);
        }

        printf("%s %2d, %4d", month(m), d, y);
    } else {
        fprintf(stderr, "Please provide one date argument to the program, formatted as mm-dd-yyyy or mm/dd/yyyy\n");
        exit(EXIT_FAILURE);
    }

    return 0;
}

改变了什么?

  • 我更改了month()的返回类型。它的数组由字符串文字支持,所以你不应该在程序的未来版本中意外修改它们,这会导致UB。
  • 我在month()中介绍了范围检查。当m太小(例如0/0/0)或太大(例如25/09/2016)时,它会返回空指针,从而阻止某些UB。
  • 我删除了有关打开文件的所有代码。您不想根据argv中的文件名打开文件,只想将argv[1]用作字符串。
  • 我介绍了一项检查,看看argv[1]是否存在。 argc包含argv的大小,如果它是2,则argv包含程序的名称及其第一个命令行参数。
  • 同样,您不想从文件中读取数据,但是将命令行参数解析为字符串,因此我将fscanf更改为sscanf

答案 1 :(得分:0)

这里我们有一个通用的解决方案。日期可以通过文件(fscanf),命令行(sscanf)或键入(scanf)传递。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* month(int m)
{
    char* months[] = { "January", "February", "March", "April", "May",
        "June", "July", "August", "September", "October",
        "November", "December" };
    return months[m - 1];
}

int main(int argc, char* argv[])
{
    int m = 0, d = 0, y = 0;

    FILE* fp;

    int wrongFormat = 0;

    if (argc > 1) 
    {
        if  ((fp = fopen(argv[1], "rb")) == NULL) 
        {
            if (sscanf(argv[1], "%d%*[/-]%d%*[/-]%d", &m, &d, &y) != 3) 
                wrongFormat = 1;
        }
        else 
        {
            if (fscanf(fp, "%d%*[/-]%d%*[/-]%d", &m, &d, &y) != 3)
                wrongFormat = 1;
        }
    }
    else 
    {
        printf("Type a date (mm-dd-yyyy) or (mm/dd/yyyy): \n");
        if (scanf("%d%*[/-]%d%*[/-]%d", &m, &d, &y) != 3)
            wrongFormat = 1;
    }

    if (wrongFormat) 
    {
        fprintf(stderr, "Not properly formatted.");
        exit(EXIT_FAILURE);
    }

    printf("%s %2d, %4d\n", month(m), d, y);

    return 0;
}