繁体   English   中英

选择长度的线,C过滤器

[英]Selecting lines with length, C filter

我正在编写一个过滤器,该过滤器应选择具有指定长度的所有行。 我最终得到了这段代码,但是我不知道如何指定n 我的意思是, n (和可选的m )应在命令提示符下替换为多行,但我不知道如何在代码中对其进行描述。 我想到了case "%d"case "%d" ,但是据我所知,不可能这样写。 到目前为止,这就是我的代码:

#include<stdio.h>
#include<string.h>

int main(int argc, char *argv[])
{
    int n;
    int m;
    char line[200];
    while(fgets(line,sizeof(line)/sizeof(char), stdin)!=NULL)
        {
            if(argc>1){
        switch(argv[0][0])
        {
        case 'n':
        strlen(line)==n;
        break;

        case '#n':
        strlen(line)<n;
        break;

        case 'n m':
        strlen(line)>=n && strlen(line)<=m;
        break;

        case 'n#':
        strlen(line) > n;
        break;
    }
    printf("%s\n", line);
          }}
    return 0;
}

您的帮助对我来说意义重大! 我真的不知道如何使它工作了。

我认为您应该在循环之外解析命令行。 假设您将要求程序的调用者在命令行上同时指定nm ,只需简单地抓取前两个参数并将其转换为整数,然后遍历标准输入即可。 像这样:

/* call this minmax.c */
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
    int n, m, len;
    char line[200];

    if (argc < 3) {
        printf("Must specify min & max line length.");
        return -1;
        }

    n = atoi(argv[1]);
    m = atoi(argv[2]);

    while(fgets(line, 200, stdin) != NULL) {
        len = strlen(line);
        if (len >=n && len <= m)
            printf(line);
        }
    return 0;
    }

假设您正在* nix上运行:

cc -ominmax minmax.c

然后用最小和最大线长来称呼它

./minmax 2 5

这将回显您键入的每行至少2个字符,但不超过5个字符。

我希望我能很好地理解您所需程序的目标,这里是代码:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main(int argc, char *argv[])
{

        int i=1,n,m; // n and m are the variable which holds 
                     // the limited length 
        if(argc>=3)
        {
          // you need to execute the program with this form
          // program.exe n m <file.txt
            n=atoi(argv[1]); // get the value of n
            m=atoi(argv[2]); // get the value of m
            printf("n=%d   m=%d\n",n,m);
        }
        char line[1000]; // this variable will hold each line of the file 
        while (fgets(line,sizeof(line),stdin)) // fgets used to read                                       
        {                                  //the lines in file till the newline 
            int  length=strlen(line)-1;
             // we decrement the length to get rid of 
             // the newline character
            if (length < n)
            {
                printf("line %d:%s status: < %d\n",i,line,n);
            }
            else if (length==n)
            {
                printf("line %d:%s status: = %d\n",i,line,n);
            }
            else if (length>n && length <=m)
            {
                printf("line %d:%s status: %d < <= %d\n",i,line,n,m);
            }
            else 
            {
                printf("line %d:%s status: > %d\n",i,line,m);
            }
            i++;
        }


    return 0;
}

万一代码不符合您的需求,我认为这已经足够了,因为它包含了您所需要的一切,因此可以作为您确切程序的支持! 希望能帮助到你 !!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM