C - Using scanf (or any other function) to get date MM/DD/YYYY in int

前端 未结 2 785
一整个雨季
一整个雨季 2020-12-22 07:08

I\'m writing a C program in class that requires us to input dates as integers in a structure defined as:

typedef struct date{

    int month;
    int day;
           


        
2条回答
  •  天命终不由人
    2020-12-22 07:44

    On a system with POSIX strptime() or a similar function, if you first read the string representation into str, you can then use:

    struct tm tm;
    strptime(str, "%m/%d/%Y", &tm);
    

    It translates to your date as follows:

    date.year = tm.tm_year;
    date.month = tm.tm_mon;
    date.day = tm.tm_mday;
    

提交回复
热议问题