C编程如何从文本文件中获取特定的字符串?

时间:2014-01-28 12:33:15

标签: c file

我的任务是创建一个输入数字的程序,然后程序将打开一个文件并检索该给定句子上的字符串。这是我正在使用的文本文件。

billy
bob
james
peter
mike
kieran
obidiah
scarlett
john
chloe
sarah
bob
leon
david
andrew
james
shawn
hannah
peter
phoebe
chris
john
mark
meg

现在,我决定更容易获得一个名字,获得一个计数值并从那里进行逆向工程,但是我完全坚持如何去做,有人可以帮忙吗?

   int main(int argc, char *argv[]) {
    int count = 1;

    char wd[20], word[20];

    FILE *fp;

    fp = fopen("Names.txt", "r");
    if (fp == NULL) {
        printf("given file doesn't exist");
        getch();
    } else {
        printf("Name: ");
        scanf("%s", word);
        fscanf(fp, "%s", wd);
        while (!feof(fp)) {
            if (strcmp(word, wd) == 0) {
                printf("%s found in the file. the given word is the %d word in the file", word, count);
                count = 0;
                break;
            } else {
                fscanf(fp, "%s", wd);
                count++;
            }
        }
        if (count != 0) {
            printf("given word is not found in the file");
        }
        getch();
    }
}

这是行代码的名称,我想要计算代号。

1 个答案:

答案 0 :(得分:0)

fgets()从文件中读取一行,因此如果您需要第n行(并且您确信知道文件中行的最大长度),则只需调用fgets() n次。你决定的方式更容易,并不容易,并且似乎没有解决所请求的任务。

从命令行读取行号,以便不完全符合您的作业规范,这:

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

#define MAX_BUFFER_SIZE 1024


/*  Parse the command line arguments  */

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

    /*  Check we have a single argument  */

    if ( argc != 2 ) {
        printf("You must enter a single integral argument.\n");
        exit(EXIT_FAILURE);
    }


    /*  Check that argument is an integer greater than zero  */

    char * endptr;
    int fline = strtol(argv[1], &endptr, 10);
    if ( *endptr || fline < 1 ) {
        printf("You must enter a positive non-zero integral argument.\n");
        exit(EXIT_FAILURE);
    }

    return fline;
}


/*  Trim trailing whitespace from a string  */

char * trim_trailing(char * buffer) {
    int idx = strlen(buffer) - 1;
    while ( idx >= 0 && isspace(buffer[idx]) ) {
        buffer[idx--] = 0;
    }
    return buffer;
}


/*  Main function  */

int main(int argc, char *argv[]) {
    int fline = parse_cmd_line(argc, argv);

    FILE * infile = fopen("data.txt", "r");
    if ( !infile ) {
        perror("Couldn't open data.txt");
        exit(EXIT_FAILURE);
    }

    int count = fline;
    char buffer[MAX_BUFFER_SIZE];

    while ( count-- > 0 ) {
        if ( fgets(buffer, MAX_BUFFER_SIZE, infile) == NULL ) {
            printf("There aren't that many lines in the file.\n");
            exit(EXIT_FAILURE);
        }
    }

    fclose(infile);

    printf("Line %d contains '%s'\n", fline, trim_trailing(buffer));

    return EXIT_SUCCESS;
}

将输出您的数据文件:

paul@MacBook:~/Documents/src/scratch$ ./fileline 1
Line 1 contains 'billy'
paul@MacBook:~/Documents/src/scratch$ ./fileline 7
Line 7 contains 'obidiah'
paul@MacBook:~/Documents/src/scratch$ ./fileline 24
Line 24 contains 'meg'
paul@MacBook:~/Documents/src/scratch$ ./fileline 25
There aren't that many lines in the file.
paul@MacBook:~/Documents/src/scratch$