简体   繁体   English

如何比较两个文件的字符串?

[英]How to compare strings of two files?

I am a new c-language programmer. 我是一名新的C语言程序员。

I have got two files. 我有两个文件。 One consists of lines like: 其中一个由以下几行组成:

84:1b:5e:a8:bf:7f 84:1b:5e:a8:bf:7f

00:8e:f2:c0:13:cc 00:8e:f2:c0:13:cc

Another consists of lines like: 另一行包括:

00-22-39 00-22-39

8C-FD-F0 8C-FD-F0

My question is how can I using C language compare first half of line in the first file with a line in the second file? 我的问题是如何使用C语言比较第一个文件中的第一行和第二个文件中的一行? Like: is 84:1b:5e equals to 8C-FD-F0 ? 像: 84:1b:5e等于8C-FD-F0吗? I know the way to create an arrays to store those lines for the further comparison. 我知道创建数组来存储这些行以进行进一步比较的方法。 But do I really need to create arrays? 但是我真的需要创建数组吗?

PS: comparison is case-insensitive PS:比较不区分大小写

You haven't been very clear about what rules constitute a match. 您还不清楚什么规则构成匹配。 But if you want to compare the byte values, then you need to parse each line, converting it to those byte values. 但是,如果要比较字节值,则需要解析每一行,并将其转换为那些字节值。

You could use variations of strtok() to get the values from each line. 您可以使用strtok()变体从每一行获取值。 However, a variation of sscanf() might be easier. 但是, sscanf()的变体可能更容易。 Once you have the binary values from each file, then you can compare them. 获得每个文件的二进制值后,就可以对其进行比较。

Read the second file completely and store the contents in a sorted array. 完全读取第二个文件,并将内容存储在已排序的数组中。 Then for each line read from the first file, binary search the sorted array to locate the match. 然后,对于从第一个文件读取的每一行,二进制搜索排序后的数组以找到匹配项。

Implementation is below. 实现如下。 It compiles with gcc. 它用gcc编译。

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

int cmp(const void * s1, const void * s2)
{
    return strcasecmp(*(char **)s1, *(char **)s2);
}

int cmp_half(const char * s1, const char * s2)
{
    int i;
    for (i = 0; i < 3; i++)
    {
        int res = strncasecmp((char *)s1+i*3, (char *)s2+i*3, 2);
        if (res != 0) return res;
    }

    return 0;
}

char * line[1024];
int n = 0;

int search(const char * s)
{
    int first, last, middle;
    first = 0;
    last = n - 1;
    middle = (first+last)/2;

    while( first <= last )
    {
        int res = cmp_half(s, line[middle]);
        if (res == 0) return middle;
        if (res > 0)
            first = middle + 1;    
        else
            last = middle - 1;

        middle = (first + last)/2;
    }
    return -1;
}

int main()
{
    FILE * f1, * f2;
    char * s;
    char buf[1024*1024], text[1024];

    f1 = fopen("file1.txt", "rt");
    f2 = fopen("file2.txt", "rt");

    s = buf;
    while (fgets(s, 1024, f2) != NULL)
    {
        line[n] = s;
        s = s+strlen(s)+1;
        n++;
    }

    qsort(line, n, sizeof(char *), cmp);

    while (fgets(text, 1024, f1) != NULL)
    {
    text[strlen(text)-1] = 0;
        int idx = search(text);
        if (idx >= 0)
        {
            printf("%s matched %s\n", text, line[idx]);
        }
        else
        {
            printf("%s not matched\n", text);
        }
    }

    return 0;
}

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

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