从用户输入的表格文本文件中读取和搜索缓冲区,并在 C 中显示正确的表格位置

问题描述 投票:0回答:1

我有一个table.txt文件:

class #1
Sect1: cat
Sect2: dog
Sect3: mouse
Sect4: bird
Sect5: squirrel

class#2
Sect1: shark
Sect2: octopus
Sect3: tuna
Sect4: eel
Sect5: dolphin

我能够读取文本文件并将其内容放入缓冲区。我提示用户输入动物,程序将搜索表格并输出表格中动物的位置。下面的例子:

Enter animal to search for: mouse
The animal mouse is located at Class #1 in Sect#3

我正在尝试找到进行搜索的最佳方法,但我不确定。谢谢大家的帮助。

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


int main()
{
    FILE* infile;
    char* buffer;
    long numbytes;
    char animal[10];
    char class[10];
    char sect[10];

    infile = fopen("table.txt", "r");

    if (NULL == infile) {
        printf("file can't be opened \n");
    }

    /* Get the number of bytes */
    fseek(infile, 0L, SEEK_END);
    numbytes = ftell(infile);

    /* reset the file position indicator to
    the beginning of the file */
    fseek(infile, 0L, SEEK_SET);

    /* grab sufficient memory for the
    buffer to hold the text */
    buffer = (char*)calloc(numbytes, sizeof(char));

    /* memory error */
    if (buffer == NULL)
        return 1;

    /* copy all the text into the buffer */
    fread(buffer, sizeof(char), numbytes, infile);
    fclose(infile);

    printf("Enter animal to search: ");
    scanf("%s", animal);

    //to do animal search
    
    printf("\nThe aninmal %s is located in %s and %s", animal, class, sect);

    /* free the memory we used for the buffer */
    free(buffer);

    // Closing the file
    fclose(infile);
    return 0;
}

我尝试将每个行项目扫描到一个数组中或使用一个结构来组织它,但不完全确定最好的方法是什么。我是在适当地构建缓冲区以进行搜索还是需要在之后解析它?

arrays c search text buffer
1个回答
0
投票

只需在缓冲区中向后搜索即可找到动物:

char *cp = buffer + numbytes - strlen(animal);
while( cp > buffer && strncmp( cp, animal, strlen( animal ) ) // non-zero = mismatch
    cp--;

// cp now points to the first letter of the animal. No duplicates, I hope.
if( cp == buffer )
    // not found... exit

// Use similar idea to search _backwards_ to find the preceding "Sect?" value
// and then again to find the "Class?" value...
// Use one variable to store the 'section' info, and one for the 'class'

您可以努力 parsing 文件,将文件分解为指针和以 null 结尾的字符串的层次结构……程序是否会“存活”不止一次搜索?

(当然,如果缓冲区是

null终止
,可以使用strstr()找到动物,然后反向搜索局部'section'和'class'进行。有很多方法可以做到这一点! )

事实上,在缓冲区的末尾分配一个额外的字节是有利的,不管:

buffer = calloc( numbytes + 1, 1 );
© www.soinside.com 2019 - 2024. All rights reserved.