-
fgets?
hi there.
could somebody tell me how to use fgets instead of scanf in the example below? thank you.
Code:
void read_date(int *yyyy, int *mm, int *dd) {
int i;
while(1) {
printf("Enter date (YYYY MM DD): ");
i = scanf("%d %d %d", yyyy, mm, dd);
if(i == 3) {
if((*yyyy < 1) || (*yyyy > 9999)) {
printf("YYYY must be between 0 and 9999\n");
continue;
}
if((*dd < 1) || (*dd > days_month[*mm-1])) {
printf("DD must be between 1 and %d for the month of %s\n", days_month[*mm-1], months[*mm-1]);
continue;
}
if((*mm < 1) || (*mm > 12)) {
printf("MM must be between 1 and 12\n");
continue;
}
} else {
continue;
}
break;
}
}
Code tags fixed by Hammer
-
oops
i'm sorry. the code tags didnt work?
-
Code tags have [] around them not <>.
fgets() is not a replacement for scanf, it is a replacement for gets. It just reads a string in, no formatting. Try using fgets to read it in then sscanf to read from the string. It is a bit safer than just using scanf.
-
Code:
char buf[BUFSIZ]; /* BUFSIZ is defined in stdio.h */
printf("Enter date (YYYY MM DD): ");
if(fgets(buf, BUFSIZ, stdin) == NULL)
{
... /* error handling */
}
else
{
i = sscanf(buf, "%d %d %d", yyyy, mm, dd);
...
}
-
thanx for fixing the code tags:)
is there anyway to do it without using sscanf either? i'm not trying to be a pain, but then it's part of the specs. please give me some clues and i'll try to work on it? thanx again.