如何在 C 编程中 strcat OPENFILENAME 参数

发布于 2024-11-14 12:50:13 字数 890 浏览 3 评论 0原文

我有一个使用 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 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
"Rule Files (*.net and *.rul)\0*.rul;*.net\0";

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2024-11-21 12:50:13

使用 "\\0" 不会做任何有用的事情,当您想要一个空字节时,它只会将文字两个字符 \0 放入字符串中。但是,C 中的字符串以 '\0' 终止,因此您无法使用 strcat 在没有一点指针算术的情况下构造 nul 分隔字符串。

因此,考虑到这些:

char filterText[] = "Rule Files (*.net and *.rul)";
char filterVal[]  = "*.rul;*.net";
char filter[100];

您需要执行以下操作:

/*
 * 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);

更好的方法是使用 malloc 分配您的 filter ,这样您就不必担心缓冲区溢出:

char *filter = malloc(strlen(filterText) + 1 + strlen(filterVal) + 1);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文