BIGGGGGGGGGG 发表于 2017-03-22 14:48

判断httpd.conf里是否有字符串Indexes

如题,C程序判断文件中是否有字符串...
看到了很多例子,但觉着都不是很完善。
想法是使用fopen打开文件,然后fgets读取文件,然后strstr判断一下,
遇到了不少问题。

请教各位大大,能否贴上代码给我瞅瞅。。。。

windoze 发表于 2017-03-22 15:18


#include <iostream>
#include <fstream>
#include <string>

int main() {
        using namespace std;
        cout << (string(istreambuf_iterator<char>(ifstream("/etc/httpd/conf/httpd.conf")), {}).find("Index")==string::npos ? "Not found\n":"Found\n");
}

你看其实C++程序也可以写的很短的。

BIGGGGGGGGGG 发表于 2017-03-22 15:55

回复 2# windoze

谢谢版主。。。处于学习中....
不过现在已经好了。贴上我的代码,帮我看看修改修改
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
      char acBuf = {0};
      char * pWord = "Indexes";
      char acOut = {0};
      char *p;

      FILE *fp = fopen("httpd.conf", "r");

      if( fp == NULL)
      {
                printf("openfile failed.\n");
                return -1;
      }

//      memset(acBuf, 0, 1024);
      while(fgets(acBuf, 1024, fp) != NULL)
      {
                p = strstr(acBuf, pWord);
                if( p == NULL)
                {
      //            return 0;
                }
                else
                {
      //            printf("%s\n", p);
                        strncpy(acOut, p, 7);
                        printf("%s\n", acOut);
                }
      }
      fclose(fp);
      return 0;
}

ljmmail 发表于 2017-03-24 07:38

可以试试这个方法:
test_word(char *word)
{
        FILE*fp = NULL;
        charcommand;
        charbuff;
        sprintf(command, "grep%s httpd.conf 2>/dev/null", word );
        fp = popen(command, "r");
        if( fp != NULL )
        {
                while(fgets(buf, sizeof(buff), fp ) != NULL )
                        printf("buff = [%s]\n", buff);
                pclose(fp);
        }
}

BIGGGGGGGGGG 发表于 2017-03-24 09:57

回复 4# ljmmail

你好,感谢解答。如图,我的问题是找到Indexes所在的<Directory “/var/www/html”> Indexes </Directory>
我现在能找到Indexes,但是不知道怎么输出/var/www/html ?求教!
我的想法是,把<Directory “/var/www/html”>视作一个整体,在读到</Directory>之前,匹配Indexes,如果有,则输出<Directory “/var/www/html”>。。 但是代码一直有问题。能否帮忙具体到代码解答一下?谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
      char acBuf;       //定义一个字符数组
      char * pWord = "Indexes";
      char acOut = {0};
      char *p;
      char strDir = {0};
      char *pDir = "<Directory>";

      FILE *fp = fopen("httpd.conf", "r");    //定义一个文件指针,打开指定文件

      if( fp == NULL) {
                printf("openfile failed.\n");
                return -1;
      }

      memset(acBuf, 0, 1024);               //初始化acbuf
      memset(strDir, 0, sizeof(strDir));

      while(fgets(strDir, 100, fp) != NULL)
      {
                if( pDir != NULL) {
//                      while( fgets(acBuf, 1024, fp) != NULL) {
                              p = strstr(acBuf, pDir);      //取出目录所在行
                              if( p == NULL) {
//                                    printf("error.\n");
                              } else {      
                                        strcpy(strDir, acBuf);
                                        continue;
                              }
                              p = strstr(acBuf, pWord);
                              if( acBuf == '#' || p == NULL)
                              {

                              } else {

                                        printf(strDir);
                              }
//                      }
                }
      }

      fclose(fp);
      return 0;
}

页: [1]
查看完整版本: 判断httpd.conf里是否有字符串Indexes