- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在这个问题中: Detecting duplicate lines on file using c我可以检测重复的行,但是我们如何从文件中删除这些行?
谢谢。
编辑:添加我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct somehash {
struct somehash *next;
unsigned hash;
char *mem;
};
#define THE_SIZE 100000
struct somehash *table[THE_SIZE] = { NULL,};
struct somehash **some_find(char *str, unsigned len);
static unsigned some_hash(char *str, unsigned len);
int main (void)
{
char buffer[100];
struct somehash **pp;
size_t len;
FILE * pFileIn;
FILE * pFileOut;
pFileIn = fopen("in.csv", "r");
pFileOut = fopen("out.csv", "w+");
if (pFileIn==NULL) perror ("Error opening input file");
if (pFileOut==NULL) perror ("Error opening output file");
while (fgets(buffer, sizeof buffer, pFileIn)) {
len = strlen(buffer);
pp = some_find(buffer, len);
if (*pp) { /* found */
fprintf(stderr, "Duplicate:%s\n", buffer);
}
else
{ /* not found: create one */
fprintf(stdout, "%s", buffer);
fprintf(pFileOut, "%s", buffer);
*pp = malloc(sizeof **pp);
(*pp)->next = NULL;
(*pp)->hash = some_hash(buffer,len);
(*pp)->mem = malloc(1+len);
memcpy((*pp)->mem , buffer, 1+len);
}
}
return 0;
}
struct somehash **some_find(char *str, unsigned len)
{
unsigned hash;
unsigned short slot;
struct somehash **hnd;
hash = some_hash(str,len);
slot = hash % THE_SIZE;
for (hnd = &table[slot]; *hnd ; hnd = &(*hnd)->next ) {
if ( (*hnd)->hash != hash) continue;
if ( strcmp((*hnd)->mem , str) ) continue;
break;
}
return hnd;
}
static unsigned some_hash(char *str, unsigned len)
{
unsigned val;
unsigned idx;
if (!len) len = strlen(str);
val = 0;
for(idx=0; idx < len; idx++ ) {
val ^= (val >> 2) ^ (val << 5) ^ (val << 13) ^ str[idx] ^ 0x80001801;
}
return val;
}
但是在输出文件中我们总是得到第一个出现的位置!
编辑 2:澄清一下:目的是查找输入文件中的所有重复项。当输入中一行有多个实例时,该行根本不应出现在输出中。。其目的不仅仅是删除该行的重复项,以便每个行仅出现一次,而是删除该行的所有实例(如果该行在输入中重复)。 p>
最佳答案
本质上,从文本文件中删除行的唯一方法是复制文件,副本中不包含这些行。通常是这个订单上的东西:
while (fgets(buffer, size, infile))
if (search(your_hashtable, buffer) == NOT_FOUND) {
fputs(line, outfile);
insert(your_hashtable, buffer);
}
如果你想节省一些存储空间,你可以存储散列而不是完整的行。理论上,这可能会因哈希冲突而失败,但如果您使用 SHA-256 之类的加密哈希,则发生冲突的可能性可能比由于 CPU 错误而导致字符串比较出错的可能性要慢。此外:如果您发现与 SHA-256 的冲突,您可能至少可以从中获得一点名气(如果不是财富)。
编辑:正如 @Zack 所提到的,哈希大小的情况基本上是决定您愿意接受的冲突机会的问题。对于加密的 256 位哈希,这种可能性非常小,几乎不值得考虑。如果将其减少为 128 位散列,则机会会增加很多,但对于大多数实际用途而言,它们仍然足够小。另一方面,如果您将其减少为 32 位 CRC,如果数据很重要,则发生冲突的可能性可能会高于我愿意接受的可能性。
我可能应该提到另一种可能性:另一种可能性是使用某种混合方式——存储诸如 32 位 CRC 之类的东西(计算速度非常)以及文件中该行开始的偏移量。如果您的文件从未超过 4G,则只需 8 个字节即可存储这两个文件。
在这种情况下,您的工作方式略有不同:首先计算 CRC,并且绝大多数情况下,当它不在文件中时,您将文件复制到输出并将这些值插入到哈希中 table 。当它已经在表中时,您将查找可能相同的行,将其读回并与当前行进行比较。如果它们匹配,您将返回到原来的位置并前进到下一行。如果它们不匹配,则将当前行复制到输出,并将其偏移量添加到哈希表中。
编辑 2:我们暂时假设该文件足够小,您可以将整个文件合理地放入内存中。在这种情况下,您可以存储一行以及它发生的行号。如果一行已存储,您可以将其行号更改为 -1,以指示它是重复的,不应出现在输出中。
在 C++ 中(因为它定义了相关的数据结构),它可能看起来像这样:
std::string line;
typedef std::map<std::string, int> line_record;
line_record lines;
int line_number = 1;
while (std::getline(line, infile)) {
line_record::iterator existing = lines.find(line);
if (existing != lines.end()) // if it was already in the map
existing->second = -1; // indicate that it's duplicated
else
lines.insert(std::make_pair(line, line_number); // otherwise, add it to map
++line_number;
}
好的,读取行,对于每一行,它检查它是否已经在 map 中。如果是,它将 line_number 设置为 -1,以指示它不会出现在输出中。如果不是,则将其及其行号插入到 map 中。
line_record::iterator pos;
std::vector<line_record::iterator> sortable_lines;
for (pos=lines.begin(); pos != lines.end(); ++pos)
if (pos->second != -1)
sortable_lines.push_back(pos);
这将 sortable_lines
设置为映射中的迭代器 vector ,因此我们只需将迭代器(本质上类似于指针)复制到这些行,而不是复制整行。然后它将迭代器复制到那里,但仅限于行号不是 -1 的行。
std::sort(sortable_lines.begin(), sortable_lines.end(), by_line_number());
struct by_line_number {
bool operator()(line_record::iterator a, line_record::iterator b) {
return a->second < b->second;
}
};
然后我们按行号对这些迭代器进行排序。
for (int i=0; i<sortable_lines.size(); i++)
outfile << sortable_lines[i]->first << "\n";
最后,我们按照原始行号的顺序将每一行复制到输出文件。
关于c - 使用 C 从文件中删除所有重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10200354/
猫f1.txt阿曼维沙尔阿杰贾伊维杰拉胡尔曼尼什肖比特批评塔夫林现在输出应该符合上面给定的条件 最佳答案 您可以在文件读取循环中设置一个计数器并打印它, 计数=0 读取行时做 让我们数一数++ if
我正在尝试查找文件 1 和文件 2 中的共同行。如果公共(public)行存在,我想写入文件 2 中的行,否则打印文件 1 中的非公共(public)行。fin1 和 fin2 是这里的文件句柄。它读
我有这个 SQL 脚本: CREATE TABLE `table_1` ( `IDTable_1` int(11) NOT NULL, PRIMARY KEY (`IDTable_1`) );
我有 512 行要插入到数据库中。我想知道提交多个插入内容是否比提交一个大插入内容有任何优势。例如 1x 512 行插入 -- INSERT INTO mydb.mytable (id, phonen
如何从用户中选择user_id,SUB(row, row - 1),其中user_id=@userid我的表用户,id 为 1、3、4、10、11、23...(不是++) --id---------u
我曾尝试四处寻找解决此问题的最佳方法,但我找不到此类问题的任何先前示例。 我正在构建一个基于超本地化的互联网购物中心,该区域分为大约 3000 个区域。每个区域包含大约 300 个项目。它们是相似的项
preg_match('|phpVersion = (.*)\n|',$wampConfFileContents,$result); $phpVersion = str_replace('"','',
我正在尝试创建一个正则表达式,使用“搜索并替换全部”删除 200 个 txt 文件的第一行和最后 10 行 我尝试 (\s*^(\h*\S.*)){10} 删除包含的前 10 行空白,但效果不佳。 最
下面的代码从数据库中获取我需要的信息,但没有打印出所有信息。首先,我知道它从表中获取了所有正确的信息,因为我已经在 sql Developer 中尝试过查询。 public static void m
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我试图在两个表中插入记录,但出现异常。您能帮我解决这个问题吗? 首先我尝试了下面的代码。 await _testRepository.InsertAsync(test); await _xyzRepo
这个基本的 bootstrap CSS 显示 1 行 4 列: Text Text Text
如果我想从表中检索前 10 行,我将使用以下代码: SELECT * FROM Persons LIMIT 10 我想知道的是如何检索前 10 个结果之后的 10 个结果。 如果我在下面执行这段代码,
今天我开始使用 JexcelApi 并遇到了这个:当您尝试从特定位置获取元素时,不是像您通常期望的那样使用sheet.getCell(row,col),而是使用sheet.getCell(col,ro
我正在尝试在我的网站上开发一个用户个人资料系统,其中包含用户之前发布的 3 个帖子。我可以让它选择前 3 条记录,但它只会显示其中一条。我是不是因为凌晨 2 点就想编码而变得愚蠢? query($q)
我在互联网上寻找答案,但找不到任何答案。 (我可能问错了?)我有一个看起来像这样的表: 我一直在使用查询: SELECT title, date, SUM(money) FROM payments W
我有以下查询,我想从数据库中获取 100 个项目,但 host_id 多次出现在 urls 表中,我想每个 host_id 从该表中最多获取 10 个唯一行。 select * from urls j
我的数据库表中有超过 500 行具有特定日期。 查询特定日期的行。 select * from msgtable where cdate='18/07/2012' 这将返回 500 行。 如何逐行查询
我想使用 sed 从某一行开始打印 n 行、跳过 n 行、打印 n 行等,直到文本文件结束。例如在第 4 行声明,打印 5-9,跳过 10-14,打印 15-19 等 来自文件 1 2 3 4 5 6
我目前正在执行验证过程来检查用户的旧密码,但问题是我无法理解为什么我的查询返回零行,而预期它有 1 行。另一件事是,即使我不将密码文本转换为 md5,哈希密码仍然得到正确的答案,但我不知道为什么会发生
我是一名优秀的程序员,十分优秀!