在这个问题中: Detecting duplicate lines on file using c i 可以检测到重复行,但是我们如何从文件中删除这些行?
谢谢。
编辑:添加我的代码:
#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:澄清:目的是在输入文件中查找所有重复项。当输入中有不止一个行的实例时,该行根本不应该出现在输出中。目的不仅是删除该行的重复项,因此每个仅出现一次,而且如果该行在输入中重复,则删除该行的所有实例。