C檔案操作 —— 在檔案中尋找關鍵字並替換

來源:互聯網
上載者:User

2011-03-30 wcdj

問題描述

有一個txt檔案,裡面的資料是這個樣子
980000001 AAE134D000
980000002 0AF2EBCE00
......
980000098 0ABDCEDB2A
......

現在有一個新資料
980000098 5ACBDC5BDC
想替換原來的那一行資料

 

原問題:http://topic.csdn.net/u/20110330/16/6e4f6070-d3c6-4a2f-8394-04e3a8cb9a7e.html

方法1

:如果每一行記錄的長度是一樣的,則可以通過fseek直接在原檔案中修改。
#include <cstdio><br />#include <cstdlib><br />#include <cstring><br />int main()<br />{<br />FILE *in = NULL;<br />if( (in = fopen("test.txt","rt+")) == NULL )// Note "rt+"<br />return -1;<br />int get1, find;<br />char str[16]="";<br />printf("Please enter number to replace: ");<br />while (rewind(stdin), scanf("%d %s", &find, str) != 2)<br />printf("Please input again: ");<br />char tmp[128] = "";<br />while (fscanf(in,"%d %*s", &get1) != EOF)<br />{<br />if (get1 == find)<br />{<br />int res = fseek(in, -20L, SEEK_CUR);// sizeof(980000001 AAE134D000) == 20B<br />itoa(find, tmp, 10);<br />strcat(tmp, " ");<br />strcat(tmp, str);// replace your string<br />strcat(tmp, "/n");<br />fputs(tmp, in);<br />break;<br />}<br />}<br />fclose(in);<br />return 0;<br />}<br />/*<br />980000001 AAE134D000<br />980000002 0AF2EBCE00<br />980000098 0ABDCEDB2A<br />*/<br />

 

修改一下代碼,如果每行固定最長為20,下面代碼允許後面的字串長度小於10,但不能大於10,否則會覆蓋下面的合法資料。實現的原理是,如果字串小於10,將後面多餘的字串用空白字元串覆蓋。( 測試過程中,發現下面代碼仍有問題 )
#include <cstdio><br />#include <cstdlib><br />#include <cstring><br />int main()<br />{<br />FILE *in = NULL;<br />if( (in = fopen("test.txt","rt+")) == NULL )// Note "rt+"<br />return -1;<br />int get1, find;<br />char str[16]="";<br />printf("Please enter number to replace: ");<br />while (rewind(stdin), scanf("%d %s", &find, str) != 2)<br />printf("Please input again: ");<br />char tmp[128] = "";<br />int beg = 0, end = 0;<br />int line = 0;<br />bool bfirst = true;<br />int nwrite = 0, nleft;<br />beg = end = ftell(in);<br />while (fscanf(in,"%d %*s", &get1) != EOF)<br />{<br />bfirst ? NULL, bfirst=false : line+=2;// because of "/r/n"<br />beg = end;<br />end = ftell(in);<br />if (get1 == find)<br />{<br />int res1 = fseek(in, -(end-beg-line), SEEK_CUR);<br />int t1 = ftell(in);<br />itoa(find, tmp, 10);<br />strcat(tmp, " ");<br />strcat(tmp, str);// replace your string<br />fputs(tmp, in);<br />int t2 = ftell(in);<br />// clear useless characters<br />nwrite = strlen(tmp);<br />if (nwrite < 20)<br />{<br />nleft = 20 -nwrite;<br />memset(tmp, ' ', nleft);<br />tmp[nleft]='/0';<br />fputs(tmp, in);<br />}<br />break;<br />}<br />}<br />fclose(in);<br />return 0;<br />}<br />/*<br />980000001 AAE134D000<br />980000002 0AF2EBCE00<br />980000098 0ABDCEDB2A<br />*/<br />

 

方法2

:如果每一行記錄的長度不同,只能輸出到另外一個檔案。

#include <cstdio><br />#include <cstdlib><br />#include <cstring><br />int main()<br />{<br />FILE *in = NULL, *out = NULL;<br />if( (in = fopen("test.txt","r+")) == NULL )<br />return -1;<br />if( (out = fopen("test2.txt","a")) == NULL )<br />return -1;<br />int get1, find;<br />char str[16]="";<br />printf("Please enter number to replace: ");<br />while (rewind(stdin), scanf("%d %s", &find, str) != 2)<br />printf("Please input again: ");<br />char get2[16] = "", tmp[128] = "";<br />while (fscanf(in,"%d %s", &get1, &get2) != EOF)<br />{<br />if (get1 == find)<br />{<br />itoa(find, tmp, 10);<br />strcat(tmp, " ");<br />strcat(tmp, str);// replace ur string<br />strcat(tmp, "/n");<br />fputs(tmp, out);<br />}<br />else<br />{<br />itoa(get1, tmp, 10);<br />strcat(tmp, " ");<br />strcat(tmp, get2);<br />strcat(tmp, "/n");<br />fputs(tmp, out);<br />}<br />}<br />fclose(in);<br />fclose(out);<br />return 0;<br />}<br />/*<br />980000001 AAE134D000<br />980000002 0AF2EBCE00<br />980000098 0ABDCEDB2A<br />*/<br />

 

參考

fopen
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

fseek
http://www.cplusplus.com/reference/clibrary/cstdio/fseek/

fputs
http://www.cplusplus.com/reference/clibrary/cstdio/fputs/

ftell
http://www.cplusplus.com/reference/clibrary/cstdio/ftell/

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.