C语言将一个字符串数组分散插入文件中,并返回插入的位置(行号)

pengfangxu8 2014-09-25 02:42:23
如题,就是将一个字符串数组分散插入一个文件中,并返回插入的行号,假如这个字符串数组有8个元素,也就是8个字符串,那就将这8个字符串插入指定的文件,每个元素一行(不是单独一行,就是在源文件的那行后面),是分散插入哈,不要连续,当然如果这个文件没有那么行的情况下是可以连续插入的,插入完成后将插入的行号返回,返回的应该是一个int的数组,元素也应该是8,就是对应的插入位置,谢谢了哈
例如:
字符串数组为:char [8][10] = {"111", "222", "333", "444", "555", "666", "777", "888"};
那么返回的整形串应该是1,3,4,6,8,11,15,20
我只是假如,当然这个要插入的文件有可能有几千行,也有可能没有8行那么多,请各位帮帮。
写成一个函数。谢谢各位大侠了,非常感谢!
...全文
195 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-09-25
  • 打赏
  • 举报
回复
不至于要我说明in.txt为源文件,out.txt为结果文件吧!我猜。
赵4老师 2014-09-25
  • 打赏
  • 举报
回复
请楼主自己动手修改为一个函数吧。
赵4老师 2014-09-25
  • 打赏
  • 举报
回复
#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;
}
pengfangxu8 2014-09-25
  • 打赏
  • 举报
回复
大侠,我试了您写的代码,没有插入文件,可能是我刚刚没有描述清楚,再请帮帮忙,谢谢您!麻烦您再看下需求,谢谢! 例如: 字符串数组为: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行那么多,请帮帮。 写成一个函数。非常感谢!
赵4老师 2014-09-25
  • 打赏
  • 举报
回复
没实际编译链接调试,不一定对。仅供参考:
#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;
}
qq120848369 2014-09-25
  • 打赏
  • 举报
回复
预处理成{行号, 字符串地址},然后洗牌吧。
pengfangxu8 2014-09-25
  • 打赏
  • 举报
回复
大侠,请给个例子,谢谢您!
赵4老师 2014-09-25
  • 打赏
  • 举报
回复
所谓修改删除文件a某位置的内容,其实是读打开文件a,再将‘a中修改删除位置之前的内容+修改删除的内容+a中修改删除位置之后的内容’保存到文件b,关闭文件a,删除文件a,将文件b改名为与之前文件a相同的名字,仅此而已。

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧