标题:strstr()函数问题
取消只看楼主
XiaoXiao_Ren
Rank: 3Rank: 3
来 自:西安
等 级:论坛游侠
威 望:1
帖 子:80
专家分:198
注 册:2013-7-17
结帖率:100%
已结贴  问题点数:10 回复次数:1 
strstr()函数问题
不知道下面注释的地方理解是否正确?——请教了


        char match[256], entry[256];
        FILE *titles_fp;
        int count = 0;
        char *found, *title, *catalog;

        mvprintw(10, 0, "Enter a string to search for in CD titles: ");
        get_string(match);  //输入如:myCD1

        titles_fp = fopen(title_file, "r");  //文件中存放形式如: cd11,myCD1,liuxing ,zhoujielun
                                             //                   cd22,myCD2,gudian ,shubote
        if (titles_fp) {
                while (fgets(entry, 256, titles_fp)) {
                        catalog = entry;
                        if (found == strstr(catalog, ",")) {  
//上面是在字符串catalog中查找首次出现的 字符串“,”的位置,并返回该位置的指针
                                *found = '\0';  //这里是将原来存放‘,’的位置改为‘\0’吗?为什么这样做?
                                title = found + 1;  //则此处 title=“myCD1,liuxing ,zhoujielun”吗?

                                if (found == strstr(title, ",")) {
                                        *found = '\0';     
                                        if (found == strstr(title, match)) {
                                                count++;
                                                strcpy(current_cd, title);
                                                strcpy(current_cat, catalog);
                                        }
                                }
                        }
                }
                fclose(titles_fp);
搜索更多相关主题的帖子: zhoujielun search Enter title count 
2013-08-17 00:16
XiaoXiao_Ren
Rank: 3Rank: 3
来 自:西安
等 级:论坛游侠
威 望:1
帖 子:80
专家分:198
注 册:2013-7-17
得分:0 
回复 2楼 rjsp
if (found == strstr(catalog, ",")) 这什么东西呀
应该是 if( (found=strstr(catalog,",")) != NULL )
当然写成 if (found = strstr(catalog, ","))  ------这里理解了 , 谢谢了大神。

可以请教一下,当我的char *found 定义之后,found是否已被分配了地址空间,
found无论什么时候都不会等于0,但在if中做判断的时候,为什么每次都可以成立。

if (found == strstr(catalog, ","))使用的 我的程序完全可以正确运行。

否极泰来
2013-08-17 20:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-419238-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013451 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved