If I understand what you are trying to do, take "YYYY/MM/DD"
and rearrange it to "YYYY_MM_DD1234"
, then continuing from my comment, all you need is to separate your input with sscanf
and create a new string with sprintf
.
All line-oriented input functions (fgets()
and POSIX getline()
) read and include the '\n'
in the buffer they fill. Your %10[^_]
includes the '\n'
following DD
input in dd
. You will need to change your sscanf
format string to:
sscanf (date, " %9[^/]/%9[^/]/%9[^\n]", yyyy, mm, dd)
Then you simply write to a new string with:
sprintf (newfmt, "%s_%s_%s1234", yyyy, mm, dd);
A short example would be:
#include <stdio.h>
#define NCHR 10 /* if you need a constant, #define one (or more) */
int main (void) {
char yyyy[NCHR],
mm[NCHR],
dd[NCHR],
date[NCHR*NCHR],
newfmt[NCHR*NCHR];
fputs ("Give the date YYYY/MM/DD: ", stdout);
if (fgets (date, sizeof date, stdin)) {
if (sscanf (date, " %9[^/]/%9[^/]/%9[^\n]", yyyy, mm, dd) != 3) {
fputs ("error: invalid date format.\n", stderr);
return 1;
}
sprintf (newfmt, "%s_%s_%s1234", yyyy, mm, dd);
puts (newfmt);
}
}
Example Use/Output
$ ./bin/newdatefmt
Give the date YYYY/MM/DD: YYYY/MM/DD
YYYY_MM_DD1234
Look things over and let me know if you have any further questions.