如何在 C 编程中 strcat OPENFILENAME 参数
我有一个使用 OPENFILENAME 的工作代码。我可以知道如何使用 strcat 动态控制其参数吗?
这个正在工作
//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");
我尝试首先使用 strcat 和 '\0' 但它只显示这样strcat(过滤器,filterText);
strcat(过滤器,"\0");
strcat(过滤器,filterVal);
strcat(过滤器,"\0");
ofn.lpstrFilter = 过滤器; \\missing \0
我尝试使用'\\0'strcat(过滤器,filterText);
strcat(过滤器,"\\0");
strcat(过滤器,filterVal);
strcat(filter,"\\0");
ofn.lpstrFilter = filter; \\现在包括\0
,
但是当我运行程序时,对话框过滤器显示如下
“规则文件(*.net 和 *.rul)\0*.rul;*.net\0”;
谢谢
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 thisstrcat (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
"Rule Files (*.net and *.rul)\0*.rul;*.net\0";
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
"\\0"
不会做任何有用的事情,当您想要一个空字节时,它只会将文字两个字符\0
放入字符串中。但是,C 中的字符串以'\0'
终止,因此您无法使用strcat
在没有一点指针算术的情况下构造 nul 分隔字符串。因此,考虑到这些:
您需要执行以下操作:
更好的方法是使用
malloc
分配您的filter
,这样您就不必担心缓冲区溢出: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 usestrcat
to construct a nul delimited string without a bit of pointer arithmetic.So, given these:
You'll need to do something like this:
A better approach would be to allocate your
filter
withmalloc
so that you don't have to worry about buffer overflows: