#include<stdio.h>
#include<time.h>
int main(){
char filepath[100];
char datevar[15];
char command[30];
struct tm *t1;
time_t now ;
time(&now);
memcpy(&t1,localtime(&now),sizeof(t1));
t1 = localtime(&now);
memset(filepath,0,sizeof(filepath));
sprintf(datevar,"%04d%02d%02d",t1->tm_year+1900,t1->tm_mon+1,t1->tm_mday);
strcpy(filepath,"abc");
strcat(filepath,"/xyx/");
strcat(filepath,datevar);
strcat(filepath,"/");
printf("filepath 1:- %s\n",filepath);
sprintf(command, "hello %s good path",filepath);
printf("filepath 2:- %s\n",filepath);
return 0;
}
在上面的程序中,两者printf
都打印不同的filepath
. 我得到的输出:-
filepath 1:- abc/xyx/20130430/
filepath 2:- h
我的问题是,如果我在sprintf
.