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> #include <cstdlib> #include <cstring> int main() { FILE *in = NULL; if( (in = fopen("test.txt","rt+")) == NULL )// Note "rt+" return -1; int get1, find; char str[16]=""; printf("Please enter number to replace: "); while (rewind(stdin), scanf("%d %s", &find, str) != 2) printf("Please input again: "); char tmp[128] = ""; while (fscanf(in,"%d %*s", &get1) != EOF) { if (get1 == find) { int res = fseek(in, -20L, SEEK_CUR);// sizeof(980000001 AAE134D000) == 20B itoa(find, tmp, 10); strcat(tmp, " "); strcat(tmp, str);// replace your string strcat(tmp, "/n"); fputs(tmp, in); break; } } fclose(in); return 0; } /* 980000001 AAE134D000 980000002 0AF2EBCE00 980000098 0ABDCEDB2A */
修改一下代码,如果每行固定最长为20,下面代码允许后面的字符串长度小于10,但不能大于10,否则会覆盖下面的合法数据。实现的原理是,如果字符串小于10,将后面多余的字符串用空格字符串覆盖。( 测试过程中,发现下面代码仍有问题 ) #include <cstdio> #include <cstdlib> #include <cstring> int main() { FILE *in = NULL; if( (in = fopen("test.txt","rt+")) == NULL )// Note "rt+" return -1; int get1, find; char str[16]=""; printf("Please enter number to replace: "); while (rewind(stdin), scanf("%d %s", &find, str) != 2) printf("Please input again: "); char tmp[128] = ""; int beg = 0, end = 0; int line = 0; bool bfirst = true; int nwrite = 0, nleft; beg = end = ftell(in); while (fscanf(in,"%d %*s", &get1) != EOF) { bfirst ? NULL, bfirst=false : line+=2;// because of "/r/n" beg = end; end = ftell(in); if (get1 == find) { int res1 = fseek(in, -(end-beg-line), SEEK_CUR); int t1 = ftell(in); itoa(find, tmp, 10); strcat(tmp, " "); strcat(tmp, str);// replace your string fputs(tmp, in); int t2 = ftell(in); // clear useless characters nwrite = strlen(tmp); if (nwrite < 20) { nleft = 20 -nwrite; memset(tmp, ' ', nleft); tmp[nleft]='/0'; fputs(tmp, in); } break; } } fclose(in); return 0; } /* 980000001 AAE134D000 980000002 0AF2EBCE00 980000098 0ABDCEDB2A */
方法2 :如果每一行记录的长度不同,只能输出到另外一个文件。
#include <cstdio> #include <cstdlib> #include <cstring> int main() { FILE *in = NULL, *out = NULL; if( (in = fopen("test.txt","r+")) == NULL ) return -1; if( (out = fopen("test2.txt","a")) == NULL ) return -1; int get1, find; char str[16]=""; printf("Please enter number to replace: "); while (rewind(stdin), scanf("%d %s", &find, str) != 2) printf("Please input again: "); char get2[16] = "", tmp[128] = ""; while (fscanf(in,"%d %s", &get1, &get2) != EOF) { if (get1 == find) { itoa(find, tmp, 10); strcat(tmp, " "); strcat(tmp, str);// replace ur string strcat(tmp, "/n"); fputs(tmp, out); } else { itoa(get1, tmp, 10); strcat(tmp, " "); strcat(tmp, get2); strcat(tmp, "/n"); fputs(tmp, out); } } fclose(in); fclose(out); return 0; } /* 980000001 AAE134D000 980000002 0AF2EBCE00 980000098 0ABDCEDB2A */
参考 : 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/