今天終於可以把檔案中重複的行給刪除了,哎,反映出來一個問題,自己對指標還是掌握的不好,遇見指標,不知道該如何進行,就像strcmp函數,參數是指標,我先前定義了char *pTmp,可是我竟然使用一下操作strcmp(pTmp[i],pTmp[j]),哎,還是得好好的學啊。
檔案中重複資料刪除行的函數是:
//刪除文本fp1中的重複資料,將最後結果放在fp2中,並將重複資料放在文本fp3中
int DeleteData(FILE *fp1, FILE *fp2, FILE *fp3)
{
int iLineCount = 0;
char linebuf[iBuflen] = {0};
iLineCount = LineCount(fp1, iLineCount); //計算文本中一共有多少行
fseek(fp1, 0, 0);
char *pFile = (char *)malloc(iLineCount * iBuflen);
char *pTmp;
pTmp = pFile; //申請動態記憶體
while (fgets(linebuf, iBuflen, fp1))
{
memcpy(pTmp, linebuf, iBuflen);
pTmp = pTmp + iBuflen;
memset(linebuf, 0, iBuflen);
}
pTmp = pFile;
for(int i=0; i<iLineCount; i++)
{
if(*pTmp) //如果該行是NULL則不操作
{
for(int j=i+1; j<=iLineCount; j++)
{
if(strcmp(pTmp, pTmp + (j - i)*iBuflen) == 0) //尋找重複行,並將下面的重複行清空
{
fprintf(fp3, pTmp + (j - i)*iBuflen);
memset(pTmp + (j - i)*iBuflen, 0, iBuflen);
}
}
fprintf(fp2,pTmp);
}
pTmp = pTmp + iBuflen;
}
free(pFile);//釋放記憶體
return 1;
}