在文件中搜索特定的线c code [英] search in a file for a specific line c code

查看:145
本文介绍了在文件中搜索特定的线c code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作C.我想问一下什么是最好的办法在文件中搜索特定的行(或多行)?是否有人可以给我一个例子。我有2个文件,我想看看,如果这两个文件是80%相同。我想到了在将文件从其他文件中的一些具体的线路之一搜索。 THX

I am working on C. I would like to ask what s the best way to search in a file for a specific line (or multiple lines)? Can someone please give me an example. I have 2 files and I would like to see if this two files are 80% identical. I thought about searching in one of the file some specific lines from the other file. Thx

我需要用C code一些例子。
这里是一个小例子

I need some example in C code. here is a small example

int compareFile(FILE* file_compared, FILE* file_checked)
{
    bool diff = 0;
    int N = 65536;
    char* b1 = (char*) calloc (1, N+1);
    char* b2 = (char*) calloc (1, N+1);
    size_t s1, s2;

    do {
        s1 = fread(b1, 1, N, file_compared);
        s2 = fread(b2, 1, N, file_checked);

        if (s1 != s2 || memcmp(b1, b2, s1)) {
            diff = 1;
            break;
        }
      } while (!feof(file_compared) || !feof(file_checked));

    free(b1);
    free(b2);

    if (diff) return 0;
    else return 1;
}

如何返回相同线路的百分比是多少?

how to return the percentage of identical lines?

推荐答案

您是否尝试过 HTTP:// WWW。 text-compare.com/ 了吗?
这是比较两个文件,​​并找到差异的简单方法。

Have you tried http://www.text-compare.com/ yet? It's an easy way to compare two files and find the differences.

如果你真的需要用C实现,为什么不能有两个文件处理程序,读取每换行的字符串,如果没有比较两个字符串,如果匹配留住他们,穿行要查找的字符的区别。

If you really need an implementation in C, why not have two file handlers, read strings per newline, compare both strings, if they match keep them, if not, walk through the characters to find the differences.

或者你也可以加载主文件,然后比较文件比较每一行的主文件中,并看看是否有任何行给出匹配> 75%,显示更改。

Or you could load the master file, and then compare the compare file to every line in the master file and see if any line gives a match > 75% and display the changes.

您可以告诉你到目前为止做了什么?

Can you show what you have done so far?

这篇关于在文件中搜索特定的线c code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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