user-0 提问: C语言将一个字符串数组分散插入文件中，并返回插入的位置（行号）

回复:
user: user-1
content: 不至于要我说明in.txt为源文件，out.txt为结果文件吧！我猜。

user: user-1
content: 请楼主自己动手修改为一个函数吧。

user: user-1
content: #include <stdio.h> #include <string.h> #include <time.h> #include <stdlib.h> char str[8][10] = {"111", "222", "333", "444", "555", "666", "777", "888"}; int  loc[8]; FILE *fi,*fo; int i,j,k,n,t; char ln[1000]; int main () {     srand(time(NULL));      fi=fopen("in.txt","r");     if (NULL==fi) {printf("Can not open file in.txt!\n");return 1;}      i=0;     while (1) {         if (NULL==fgets(ln,1000,fi)) break;         i++;     }     n=i;     printf("n=%d\n",n);     if (n==0) n=1;      for (i=0;i<8;i++) {         k=0;         while (1) {             loc[i]=rand()%n;             for (j=0;j<i;j++) {                 if (loc[j]==loc[i]) break;             }             if (j>=i || k>100) break;             k++;         }     }      for (i=0;i<7;i++) {         for (j=i+1;j<8;j++) {             if (loc[i]>loc[j]) {                 t=loc[i];loc[i]=loc[j];loc[j]=t;             }         }     }      for (i=0;i<8;i++) printf("%d ",loc[i]+1);     printf("\n");      fo=fopen("out.txt","w");     if (NULL==fo) {printf("Can not create file out.txt!\n");fclose(fi);return 2;}      rewind(fi);      k=0;     j=0;     i=0;     while (1) {         if (NULL==fgets(ln,1000,fi)) break;         if (i==loc[j]) {             ln[strlen(ln)-1]=0;//去掉末尾的'\n'             fprintf(fo,"%s%s",ln,str[k++]);             while (1) {                 if (j<8 && loc[j+1]==loc[j]) {                     fprintf(fo,"%s",str[k++]);                     j++;                 } else break;             }             fprintf(fo,"\n");             if (j<8) j++;             else break;         } else {             fprintf(fo,"%s",ln);         }         i++;     }     if (k<8) {         for (i=k;i<8;i++) fprintf(fo,"%s",str[i]);         fprintf(fo,"\n");     }      fclose(fo);     fclose(fi);     return 0; }

user: user-0
content: 大侠，我试了您写的代码，没有插入文件，可能是我刚刚没有描述清楚，再请帮帮忙，谢谢您！麻烦您再看下需求，谢谢！ 例如： 字符串数组为：char [8][10] = {"111", "222", "333", "444", "555", "666", "777", "888"}; 源文件为： aaaaaaaaaaaaaaa bbbbbbbbbbbbbbb ccccccccccccccc ddddddddddddddd eeeeeeeeeeeeeee fffffffffffffff ggggggggggggggg hhhhhhhhhhhhhhh iiiiiiiiiiiiiii kkkkkkkkkkkkkkk lllllllllllllll mmmmmmmmmmmmmmm nnnnnnnnnnnnnnn ooooooooooooooo ppppppppppppppp qqqqqqqqqqqqqqq rrrrrrrrrrrrrrr sssssssssssssss ttttttttttttttt  插入后的文件假如为： aaaaaaaaaaaaaaa111 bbbbbbbbbbbbbbb ccccccccccccccc222 ddddddddddddddd333 eeeeeeeeeeeeeee fffffffffffffff444 ggggggggggggggg hhhhhhhhhhhhhhh555 iiiiiiiiiiiiiii kkkkkkkkkkkkkkk lllllllllllllll666 mmmmmmmmmmmmmmm nnnnnnnnnnnnnnn ooooooooooooooo ppppppppppppppp777 qqqqqqqqqqqqqqq rrrrrrrrrrrrrrr sssssssssssssss ttttttttttttttt 888  那么返回的整形串应该是1,3,4,6,8,11,15,20 我只是假如，当然这个要插入的文件有可能有几千行，也有可能没有8行那么多，请帮帮。 写成一个函数。非常感谢！

user: user-1
content: 没实际编译链接调试，不一定对。仅供参考： #include <stdio.h> #include <string.h> #include <time.h> #include <stdlib.h> char str[8][10] = {"111", "222", "333", "444", "555", "666", "777", "888"}; int  loc[8]; FILE *fi,*fo; int i,j,n,t; char ln[1000]; int main () {     srand(time(NULL));      fi=fopen("in.txt","r");     if (NULL==fi) {printf("Can not open file in.txt!\n");return 1;}      i=0;     while (1) {         if (NULL==fgets(ln,1000,fi)) break;         i++;     }     n=i;      for (i=0;i<8;i++) {         k=0;         while (1) {             loc[i]=rand()%(n+1);             for (j=0;j<i;j++) {                 if (loc[j]==loc[i]) break;             }             if (j>=i || k>100) break;             k++;         }     }      for (i=0;i<7;i++) {         for (j=i+1;j<8;j++) {             if (loc[i]>loc[j]) {                 t=loc[i];loc[i]=loc[j];loc[j]=t;             }         }     }      for (i=0;i<8;i++) printf("%d ",loc[i]+1);     printf("\n");      fo=fopen("out.txt","w");     if (NULL==fo) {printf("Can not create file out.txt!\n");fclose(fi);return 2;}      rewind(fi);      j=0;     i=0;     while (1) {         if (NULL==fgets(ln,1000,fi)) break;         if (i==loc[j]) {             ln[strlen(ln)-1]=0;//去掉末尾的'\n'             fprintf(fo,"%s%s\n",ln,str[j]);             j++;         } else {             fprintf(fo,"%s",ln);         }         i++;     }      fclose(fo);     fclose(fi);     return 0; }

user: user-2
content: 预处理成{行号, 字符串地址}，然后洗牌吧。

user: user-0
content: 大侠，请给个例子，谢谢您！

user: user-1
content: 所谓修改删除文件a某位置的内容，其实是读打开文件a，再将‘a中修改删除位置之前的内容＋修改删除的内容＋a中修改删除位置之后的内容’保存到文件b，关闭文件a，删除文件a，将文件b改名为与之前文件a相同的名字，仅此而已。