| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 823 人关注过本帖, 1 人收藏
标题:多个文件的链接问题
只看楼主 加入收藏
yahwei
Rank: 7Rank: 7Rank: 7
来 自:湖~
等 级:黑侠
威 望:3
帖 子:145
专家分:644
注 册:2011-11-10
收藏
得分:2 
回复 楼主 ZMX900507
简单描述下我的思路:文件1存放数字,文件2存放频率,文件3存放满足条件的字符。不需要数组,每次从文件1和文件2各读取一行,取乘积判断,满足条件就存放到文件3。

[qq]949654600[/qq]
2015-01-10 14:41
ZMX900507
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2015-1-8
收藏
得分:0 
回复 11楼 yahwei
谢谢你!  我想用你的方法试一下  先编了个小程序   把ha1 的内容一行一行的读出来在存入新的文件hap2 文件中 可是hap2 文件只存ha1文件的最后一行  找不到原因  程序如下
#include <stdio.h>
   
  void main()
   {
       FILE *fp,*fp1;
       char StrLine[1024];
       if((fp = fopen("ha2.txt","r")) == NULL) 读
       {
          printf("error!");  
      }
  
      while (!feof(fp))
      {
          fgets(StrLine,1024,fp);  //读取一行
         printf("%s", StrLine); //输出
          if ((fp1 = fopen("hap2.txt","w"))==NULL)
            {
                printf("the file can not open..");
            }
         else
         {
             printf("hahha\n");
            fprintf(fp1,"%s", StrLine);
            fclose(fp1);
         }
      }
      fclose(fp);                     
  }
2015-01-10 16:41
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:2 
还有点问题吧

我不是砖家,要努力成为砖家。
2015-01-10 17:00
ZMX900507
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2015-1-8
收藏
得分:0 
回复 13楼 砖家的谎言
  没找到原因,请大师指点
2015-01-10 18:04
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:2 
代码太长太烂 就不作内存开辟和文件打开错误的检测了
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define q 0.03

void haLen(FILE *fp, int *len)
{
    int a, b;
    char *fmt = "%d\t%d";

    while(!feof(fp)) {
        fscanf(fp, fmt, &a, &b);
        *len++ = b - a + 1;
    }
}

int clacLine(FILE *fp, int len)
{
    char c[80];
    int cur = 0;

    while(!feof(fp)) {
        fgets(c, 80, fp);

        if((c[len] == '\n' || c[len] == '\0')
                && c[0] != '\n' && c[0] != '\0') {
            cur++;
            c[len] = '\0';
        }
    }

    return cur;
}

void readFileH(FILE *fp, int cur, int len, char (*p)[len + 1])
{
    int i;
    rewind(fp);

    for(i = 0; i < cur; i++) {
        fgets(*(p + i), len + 1, fp);
        fgetc(fp);
    }
}

void readFileF(FILE *fp, int cur, double *p)
{
    int i;

    for(i = 0; i < cur; i++) {
        fscanf(fp, "%lf", p + i);
        fgetc(fp);
    }
}

void newfile(FILE *fpNew, int len, int cur,
             char (*pHa)[len + 1], double *pF, char *strNum)
{
    for(int i = 0; i < cur ; i++) {
        if((atoi(*(pHa + i)) * *(pF + i)) > q) {
            fwrite(pHa + i, sizeof(char), strlen(*(pHa + i)), fpNew);
            sprintf(strNum, "%lf", *(pF + i));
            fwrite(strNum, sizeof(char), strlen(strNum), fpNew);
            fprintf(fpNew, "\n");
        }
    }
}

int main(void)
{
    int len[4], cur;
    char strNum[8];
    FILE *fpQiZhi = fopen("fenkuailianjieqizhi.txt", "r");
    FILE *fpHa1 = fopen("ha1.txt", "r");
    FILE *fpHa2 = fopen("ha2.txt", "r");
    FILE *fpHa3 = fopen("ha3.txt", "r");
    FILE *fpHa4 = fopen("ha4.txt", "r");
    FILE *fpF1 = fopen("f1.txt", "r");
    FILE *fpF2 = fopen("f1.txt", "r");
    FILE *fpF3 = fopen("f1.txt", "r");
    FILE *fpF4 = fopen("f1.txt", "r");
    FILE *fpNew = fopen("new.txt", "wb");
    haLen(fpQiZhi, len);
    /*--------------------------------------------*/
    cur = clacLine(fpHa1, len[0]);
    char (*pHa)[len[0] + 1] = malloc(cur * (len[0] + 1) * sizeof(char));
    double *pF = malloc(cur * sizeof(double));
    readFileH(fpHa1, cur, len[0], pHa);
    readFileF(fpF1, cur, pF);
    newfile(fpNew, len[0], cur, pHa, pF, strNum);
    /*--------------------------------------------*/
    cur = clacLine(fpHa2, len[1]);
    realloc(pHa, cur * (len[1] + 1) * sizeof(char));
    realloc(pF, cur * sizeof(double));
    readFileH(fpHa2, cur, len[1], pHa);
    readFileF(fpF2, cur, pF);
    newfile(fpNew, len[1], cur, pHa, pF, strNum);
    /*--------------------------------------------*/
    cur = clacLine(fpHa3, len[2]);
    realloc(pHa, cur * (len[2] + 1) * sizeof(char));
    realloc(pF, cur * sizeof(double));
    readFileH(fpHa3, cur, len[2], pHa);
    readFileF(fpF3, cur, pF);
    newfile(fpNew, len[2], cur, pHa, pF, strNum);
    /*--------------------------------------------*/
    cur = clacLine(fpHa4, len[3]);
    realloc(pHa, cur * (len[3] + 1) * sizeof(char));
    realloc(pF, cur * sizeof(double));
    readFileH(fpHa3, cur, len[3], pHa);
    readFileF(fpF3, cur, pF);
    newfile(fpNew, len[3], cur, pHa, pF, strNum);
    /*--------------------------------------------*/
    free(pF);
    free(pHa);
    fclose(fpQiZhi);
    fclose(fpHa1);
    fclose(fpHa2);
    fclose(fpHa3);
    fclose(fpHa4);
    fclose(fpF1);
    fclose(fpF2);
    fclose(fpF3);
    fclose(fpF4);
    fclose(fpNew);
    return 0;
}


[ 本帖最后由 longwu9t 于 2015-1-11 02:48 编辑 ]

Only the Code Tells the Truth             K.I.S.S
2015-01-10 20:58
ZMX900507
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2015-1-8
收藏
得分:0 
回复 15楼 longwu9t
谢谢你!我在vc6.0上运行了 但是编译时候一直显示错误 改了一会没有找到错误的地方
ha1-7和f1-7文件每行的结尾都是一个‘\n’
最好要循环的比较因为这次“fenkuailianjieqizhi.txt”里面显示的是四行(说明ha1-7和f1-7文件各四个)但是下一次“fenkuailianjieqizhi.txt”里面可能是8行(说明ha1-7和f1-7文件各8个)
2015-01-11 11:24
yahwei
Rank: 7Rank: 7Rank: 7
来 自:湖~
等 级:黑侠
威 望:3
帖 子:145
专家分:644
注 册:2011-11-10
收藏
得分:0 
以下是引用ZMX900507在2015-1-10 16:41:42的发言:

谢谢你!  我想用你的方法试一下  先编了个小程序   把ha1 的内容一行一行的读出来在存入新的文件hap2 文件中 可是hap2 文件只存ha1文件的最后一行  找不到原因  程序如下
#include  
   
  void main()
   {
       FILE *fp,*fp1;
       char StrLine[1024];
       if((fp = fopen("ha2.txt","r")) == NULL) 读
       {
          printf("error!");  
      }
  
      while (!feof(fp))
      {
          fgets(StrLine,1024,fp);  //读取一行
         printf("%s", StrLine); //输出
          if ((fp1 = fopen("hap2.txt","w"))==NULL)
            {
                printf("the file can not open..");
            }
         else
         {
             printf("hahha\n");
            fprintf(fp1,"%s", StrLine);
            fclose(fp1);
         }
      }
      fclose(fp);                     
  }
这句“fp1 = fopen ( "hap2.txt", "w" )”要放在循环体外面,否则,你每次都没有保存就重新打开文件,那以前的内容肯定会丢失。

[qq]949654600[/qq]
2015-01-11 12:41
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:0 
都发过来看看

我不是砖家,要努力成为砖家。
2015-01-11 14:04
ZMX900507
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2015-1-8
收藏
得分:0 
回复 18楼 砖家的谎言
这是我编写的代码  由于初学 所以用的都是简单的代码  编译通过了 但是占用的内存太大了
当 q的值很小时 运行就出差错了  显示内存无法读
想改进的方法是   把文本文件中相同的行数去掉(这一段代码想了好久没有编出来)
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#include<string.h>
#define m 7
#define q 0.003
void main()
{
    FILE *fp,*fp1;
    int i,j=0,Individual=0,SNP=0,x,t,k,p,g,h1;
    int a[100][5]={0};
    char str1[700][200]={0};
    char str2[700][200]={0};
    char str3[700][200]={0};
    char mid,fname[20],finame[20];
    double fr[700]={0},fr1[700]={0},fr2[700]={0};
    int b[500]={0},c[100]={0};
    //int
    if((fp=fopen("fenkuailianjieqizhi.txt","r"))==NULL)/*确定每一个文本文件的起止字符*/
    {
        printf("cannot open this file\n");
        exit(0);
    }
    else
    {
        for(i=0;i<m;i++)
            for(j=0;j<2;j++)
                fscanf(fp,"%d",&a[i][j]);
            fclose(fp);
    }
    for(i=1;i<=m;i++)/*确定每个文本文件有多少行*/
    {
        Individual=0;
        sprintf(fname, "ha%d.txt", i);
        if((fp=fopen(fname,"r"))==NULL)
        {
            printf("Can not open the file!\n");
        }
        while(!feof(fp))
        {
            mid=fgetc(fp);
            if(mid=='\n')
                Individual++;
        }
        c[i-1]=Individual;
    }
    for(i=1;i<=m;i++)
        printf("%d\t",c[i-1]);
    printf("\n");
    if ((fp = fopen("ha1.txt","r"))==NULL)/*将每个文本文件的内容赋给数组*/
        {
            printf("cannot open this file\n");
            exit(0);
        }
    else
    {
        for(i=0;i<c[0];i++)
            for(j=0;j<a[0][1]-a[0][0]+2;j++)
                fscanf(fp,"%c",&str1[i][j]);
            fclose(fp);
    }
    /*for(i=0;i<c[0];i++)
    {
        for(j=0;j<a[0][1]-a[0][0]+1;j++)
            printf("%c",str1[i][j]);
            printf("\n");
    }*/
    if ((fp = fopen("f1.txt","r"))==NULL)
    {
        printf("cannot open this file\n");
        exit(0);
    }
    else
    {
        for(i=0;i<c[0];i++)
            fscanf(fp,"%lf",&fr[i]);
        fclose(fp);
    }
    //for(i=0;i<c[0];i++)
            //printf("%lf",fr[i]);
    t=c[0];
    h1=a[0][1]-a[0][0]+1;
    for(i=2;i<=m;i++)
    {
        sprintf(fname, "ha%d.txt", i);
        if ((fp = fopen(fname,"r"))==NULL)
        {
            printf("cannot open this file\n");
            exit(0);
        }
        else
        {
            for(x=0;x<c[i-1];x++)
                for(j=0;j<a[i-1][1]-a[i-1][0]+2;j++)
                    fscanf(fp,"%c",&str2[x][j]);
                fclose(fp);
            /*for(x=0;x<c[i-1];x++)
            {
                for(j=0;j<a[i-1][1]-a[i-1][0]+1;j++)
                    printf("%c",str2[x][j]);
                printf("\n");
            }*/
            sprintf(fname, "f%d.txt", i);
            if ((fp = fopen(fname,"r"))==NULL)
            {
                printf("cannot open this file\n");
                exit(0);
            }
            else
            {
                for(x=0;x<c[i-1];x++)
                    fscanf(fp,"%lf",&fr1[x]);
                fclose(fp);
            }
            k=0;
            for(x=0;x<t;x++)
            {
                for(j=0;j<c[i-1];j++)
                {
                    if(fr[x]*fr1[j]>q)
                    {
                        fr2[k]=fr[x]*fr1[j];
                        for(p=0;p<h1;p++)
                        {
                            str3[k][p]=str1[x][p];
                            //printf("%c\t",str3[k][p]);
                            //str3[k][2+t]=str2[j][t];   
                        }
                        for(g=0;g<a[i-1][1]-a[i-1][0]+2;g++)
                            str3[k][h1+g]=str2[j][g];
                        //printf("%c\t",str3[k][a[0][1]-a[0][0]+1+g]);
                        k++;
                    }
                }
            }
            t=k;
            for(x=0;x<t;x++)
            {
                for(j=0;j<h1+a[i-1][1]-a[i-1][0]+2;j++)
                    str1[x][j]=str3[x][j];
            }
            for(x=0;x<t;x++)
            {
                for(j=0;j<h1+a[i-1][1]-a[i-1][0]+2;j++)
                    printf("%c",str1[x][j]);
            }
            //printf("%d\t",k);
            for(x=0;x<k;x++)
            {
                fr[x]=fr2[x];
            }
            /*for(x=0;x<k;x++)
            {
                printf("%lf\t",fr[x]);
            }*/
            sprintf(finame, "h%d.txt", i);
            if ((fp1 = fopen(finame,"w"))==NULL)
            {
                printf("the file can not open..");
            }
            else
            for(x = 0;x < t;x++)
            {
                for(j= 0;j < h1+a[i-1][1]-a[i-1][0]+2;j++)
                {
                    fprintf(fp1,"%c",str1[x][j]);
                }
            }
         fclose(fp1);
         h1=h1+(a[i-1][1]-a[i-1][0])+1;
         printf("%d\t",h1);
        }
    }
}
2015-01-11 20:59
快速回复:多个文件的链接问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.103187 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved