how to strcat in OPENFILENAME paramaeters in C programming
I have a working code using OPENFILENAME. May i know how to use strcat to dynamically control the its parameters
this one is working
//ofn.lpstrFilter = "Rule Files (*.net and *.rul)\0*.rul;*.net\0";
char filter[100];
char filterText[100];
char filterVal[100];
strcpy(filterText, "Rule Files (*.net and *.rul)");
strcpy(filterVal, "*.rul;*.net");
I tried using strcat first with '\0' but it only only shows like this
strcat (filter, filterText);
strcat (filter,"\0");
strcat (filter,filterVal);
strcat (filter,"\0");
ofn.lpstrFilter = filter; \\missing \0
And I tried using '\\0'
strcat (filter, filterText);
strcat (filter,"\\0");
strcat (filter,filterVal);
strcat (filter,"\\0");
ofn.lpstrFilter = filter; \\now includes the\0
but when i run the program the dialogue box filter shows like this
"开发者_C百科Rule Files (*.net and *.rul)\0*.rul;*.net\0";thanks
Using "\\0"
won't do anything useful, that will just put the literal two characters \0
in your string when you want a nul byte. However, strings in C are terminated by '\0'
so you can't use strcat
to construct a nul delimited string without a bit of pointer arithmetic.
So, given these:
char filterText[] = "Rule Files (*.net and *.rul)";
char filterVal[] = "*.rul;*.net";
char filter[100];
You'll need to do something like this:
/*
* The first one is a straight copy.
*/
strcpy(filter, filterText);
/*
* Here we need to offset into filter to right after the
* nul byte from the first copy.
*/
strcpy(&filter[strlen(filterText) + 1], filterVal);
A better approach would be to allocate your filter
with malloc
so that you don't have to worry about buffer overflows:
char *filter = malloc(strlen(filterText) + 1 + strlen(filterVal) + 1);
精彩评论