文本中重複資料刪除行

來源:互聯網
上載者:User

    今天終於可以把檔案中重複的行給刪除了,哎,反映出來一個問題,自己對指標還是掌握的不好,遇見指標,不知道該如何進行,就像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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.