I got some troubles trying to copy char to char…
Here’s args[] definition :
char* args[6];
if (1 == ParseQuery(request, &args[0])) ....
#note : args[ARG_DATE] = "201204090600"
Here’s d definition :
struct date {
char a[4];
char m[2];
char j[2];
char h[4];
};
date d;
This is what I’m trying to do :
int TileServe::ParseQuery(FastCGIRequest& request, char** args) {
for (int i=0; i<4; i++) {
d.a[i] = args[ARG_DATE][i];
d.h[i] = args[ARG_DATE][i+8];
}
for (int i=0; i<2; i++) {
d.m[i] = args[ARG_DATE][i+4];
d.j[i] = args[ARG_DATE][i+6];
}
strcat(filename, "/");
strcat(filename, d.a);
strcat(filename, "/");
strcat(filename, d.m);
...
printf("%s\n", filename);
I get :
./data/alpes/201204090600/04090600/090600/0600/alpes_201204090600.nc
I’m expecting :
./data/alpes/2012/04/09/06/alpes_201204090600.nc
What am I doing wrong ?
Most C functions expect null-terminated strings. Your substrings (d.a, d.m, …) are not null-terminated. Hence
strcatis appending everything up until the first null-character it finds in memory after your substrings.One fix is to lengthen the char arrays by one and place the
'\0'character at the end of your substrings.Another fix would be to use
strncatinstead ofstrcatto append a specified number of characters.