扫描一个关键字的文件和打印匹配行 [英] Scan a file for a keyword and print matching lines

查看:263
本文介绍了扫描一个关键字的文件和打印匹配行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以扫描文件为一个字,然后打印包含C编程的字行?

我试图找到一种方法来扫描一个关键字的文件,然后只打印包含该字线。我开了一个非常艰难中起步。以下是我有:

 的#include<&stdio.h中GT;无效getDsk(无效);无效getDsk()
{
FILE * FP;
焦炭结果[1000];
FP =的popen(磁盘工具列表,R);
FREAD(结果,1,sizeof的(结果),FP​​);
FCLOSE(FP);而(!的feof(FP)){
        如果(//行包含测试。)
        {
            //显示线。
        }
    }
}INT主(INT ARGC,为const char * argv的[])
{
    getDsk();
    返回0;
}

编辑::此做了我所需要的。

 的#include<&stdio.h中GT;无效getDsk(无效);无效getDsk()
{
    的printf(您可用Windows安装有:\\ n);
    FILE *计划生育=的popen(磁盘工具列表,R);
    焦线[1024];
    而(与fgets(线,的sizeof(线),FP))
    {
        如果(的strstr(行,微软))
        {
            的printf(%S,行);
        }
        //如果行包含文本,打印
    }
}INT主(INT ARGC,为const char * argv的[])
{
    getDsk();
    返回0;
}


解决方案

有几件事情:

1)你应该pretty多从来不写而(!的feof(FP)){...} 这是一个模式,因为的feof 只会返回真正的读取的尝试。所以,你是开放的偏离一个错误。

2)为什么赫克你这样做?

 计划生育=的popen(磁盘工具列表,R);
FREAD(结果,1,sizeof的(结果),FP​​);
FCLOSE(FP);

您正在阅读 1000 然后将文件的字符的结束,以后的存根code充当如果它计划在阅读该文件的更多一些......是没有意义的。

3)这里是一个合理的方法:

 字符行[1024];
而(与fgets(线,的sizeof(线),FP)){
    //如果行包含文本,打印
}

什么是怎么回事是与fgets 将读取一行返回非 - NULL 如果成功。 ( NULL 被视为假,非 - NULL 在测试中使用时为真)。

因此​​,它是说:虽然我们可以成功读取一条线,请执行下列操作,这真的是你想要的。另外,我想提出的假设是没有线将超过 1024 字符,这通常是一个合理的假设,如果不是这样的话,那么你就需要做相应的调整。

How can I scan a file for a word and then print the line containing that word in C programming?

I am trying to find a way to scan a file for a keyword and then print only the line containing that word. I am off to a very rough start. Here is what I have:

#include <stdio.h>

void getDsk (void);

void getDsk ()
{
FILE* fp;
char result [1000];
fp = popen("diskutil list","r");
fread(result,1,sizeof(result),fp);
fclose (fp);

while(!feof(fp)) {
        if(//line contains "Test".)
        {
            //show line.
        }
    }
}

int main(int argc, const char * argv[])
{
    getDsk();
    return 0;
}

EDIT: This did what I needed.

#include <stdio.h>

void getDsk (void);

void getDsk ()
{
    printf("Your available Windows installations are:\n");
    FILE* fp = popen("diskutil list","r");
    char line[1024];
    while(fgets(line, sizeof(line), fp)) 
    {
        if (strstr(line,"Microsoft")) 
        {
            printf("%s", line);
        }
        // if line contains the text, print it
    }
}

int main(int argc, const char * argv[])
{
    getDsk();
    return 0;
}

解决方案

A few things:

1) You should pretty much never write while(!feof(fp)) { ... } It is a broken pattern because feof will only return true after a read attempt. So you are open to off by one errors.

2) why the heck are you doing this?

fp = popen("diskutil list","r");
fread(result,1,sizeof(result),fp);
fclose (fp);

You are reading 1000 chars of the file then closing it, then later your stub code acts as if it plans on reading the file some more... makes no sense.

3) here's a reasonable approach:

char line[1024];
while(fgets(line, sizeof(line), fp)) {
    // if line contains the text, print it
}

what is going on here is fgets will read a line and return non-NULL if it succeeds. (NULL is treated as "false", non-NULL is "true" when used in a test).

So it is saying "while we could successfully read a line, do the following", which is really what you want. Also, I am making the assumption that no lines will exceed 1024 characters which is usually a reasonable assumption, if this is not the case, then you will need to adjust accordingly.

这篇关于扫描一个关键字的文件和打印匹配行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆