如何在C编程中将出生日期格式作为字符串输入
how to take a birth date format as a input with string in c programming
将日期格式作为输入的最佳方法是什么。像这样..
dd/mm/yyyy。
我不喜欢使用 scanf("%d/%d/%d.........);
你的问题不是很清楚
如果你想知道如何使用 scanf 获取日期输入,这里是如何完成的。
int d,m,y; //This is your code.
scanf("%d/%d/%d",&d,&m,&y); //Now this tells that scanf expect input in this format.
如果您输入 23/4/12
,这将在 d
中存储 23
,在 m
中存储 4
,在 [=] 中存储 12
18=].
切勿使用 gets()
和 scanf()
进行输入,因为它们不检查缓冲区溢出,并且 gets()
已从很久以前的标准方式中删除。这是众所周知的安全风险。
改为使用 fgets()
。注意 fgets()
还存储结束换行符,要删除它我使用了下面的方法。
使用 fgets()
获取此输入。
#include <stdio.h>
int main(){
char date[10];
fgets(date,10,stdin);
int i = 0;
//Now For Removing new line character from end of the string.
while(date[i]!='[=11=]'){
if(date[i]=='\n'){
date[i] = '[=11=]';
break;
}
i++;
}
}
首先你应该避免gets()
以防止缓冲区溢出。
而是使用最安全的 fgets()
char *fgets(char *s, int size, FILE *stream)
fgets()
reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq[=16=]aq) is stored after the last character in the buffer.
那你可以用int sscanf(const char *str, const char *format, ...);
哪个
reads its input from the character string pointed to by str.
这是一个示例程序:
#include <stdio.h>
#define MAXLEN 10
int main(int argc, char **argv)
{
char date_of_birth[MAXLEN];
int day_of_birth, month_of_birth, year_of_birth;
fgets(date_of_birth, MAXLEN, stdin);
sscanf(date_of_birth,"%d %*c %d %*c %d", &day_of_birth, &month_of_birth, &year_of_birth);
printf("\nDay of birth : %d\nMonth of birth : %d\nYear of birth : %d\n", day_of_birth, month_of_birth, year_of_birth);
return 0;
}
将日期格式作为输入的最佳方法是什么。像这样.. dd/mm/yyyy。 我不喜欢使用 scanf("%d/%d/%d.........);
你的问题不是很清楚
如果你想知道如何使用 scanf 获取日期输入,这里是如何完成的。
int d,m,y; //This is your code.
scanf("%d/%d/%d",&d,&m,&y); //Now this tells that scanf expect input in this format.
如果您输入 23/4/12
,这将在 d
中存储 23
,在 m
中存储 4
,在 [=] 中存储 12
18=].
切勿使用 gets()
和 scanf()
进行输入,因为它们不检查缓冲区溢出,并且 gets()
已从很久以前的标准方式中删除。这是众所周知的安全风险。
改为使用 fgets()
。注意 fgets()
还存储结束换行符,要删除它我使用了下面的方法。
使用 fgets()
获取此输入。
#include <stdio.h>
int main(){
char date[10];
fgets(date,10,stdin);
int i = 0;
//Now For Removing new line character from end of the string.
while(date[i]!='[=11=]'){
if(date[i]=='\n'){
date[i] = '[=11=]';
break;
}
i++;
}
}
首先你应该避免gets()
以防止缓冲区溢出。
而是使用最安全的 fgets()
char *fgets(char *s, int size, FILE *stream)
fgets()
reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq[=16=]aq) is stored after the last character in the buffer.
那你可以用int sscanf(const char *str, const char *format, ...);
哪个
reads its input from the character string pointed to by str.
这是一个示例程序:
#include <stdio.h>
#define MAXLEN 10
int main(int argc, char **argv)
{
char date_of_birth[MAXLEN];
int day_of_birth, month_of_birth, year_of_birth;
fgets(date_of_birth, MAXLEN, stdin);
sscanf(date_of_birth,"%d %*c %d %*c %d", &day_of_birth, &month_of_birth, &year_of_birth);
printf("\nDay of birth : %d\nMonth of birth : %d\nYear of birth : %d\n", day_of_birth, month_of_birth, year_of_birth);
return 0;
}