Jane爱蓝黑墨水 发表于 2014-1-21 10:05:04

又来寻找大神了、、、、、

本帖最后由 Jane爱蓝黑墨水 于 2014-1-21 10:25 编辑

有两个磁盘文件“A"和"B",各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件”C"中去。
按照我下面这样打出来又不对啊。。求大神指教。。我还在摸索过程中。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
    FILE *fp;
    char a;
    char ch;
    int i=0,j,n,temp;
    if((fp=fopen("A.txt","r"))==NULL)
    {
      printf("cannot open this file\n");
      exit(0);
    }
    ch=fgetc(fp);
    while(ch!=feof(fp))
    {
      a=ch;
      putchar(a);
      i++;
    }
    fclose(fp);

    if((fp=fopen("B.txt","r"))==NULL)
    {
      printf("cannot open this file\n");
      exit(0);
    }
    ch=fgetc(fp);
    while(ch!=feof(fp))
    {
      a=ch;
      putchar(a);
      i++;
    }
    for(i=0;i<n;i++)
    {
      for(j=i+1;j<n;j++)
      {
            if(a>a)
            {
                temp=a;
                a=a;
                a=temp;
            }
      }
    }
    if((fp=fopen("C.txt","w"))==NULL)
    {
      printf("cannot open this file\n");
      exit(0);
    }
    printf("C is:\n");
    for(i=0;i<n;i++)
    {
      fputc(a,fp);
      putchar(a);
    }
    fclose(fp);
}


牛头的主人 发表于 2014-1-21 12:46:18

//根据你的要求,写了个简短的,你的代码实在看不懂 什么意思,把简单的问题复杂化了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
    FILE *fp;
    char a;
        char b;

    if((fp=fopen("d:\\A.txt","r"))==NULL)
    {
      printf("cannot open this file\n");
      exit(0);
    }
    fgets(a,strlen(a),fp);

    fclose(fp);

    if((fp=fopen("d:\\B.txt","r"))==NULL)
    {
      printf("cannot open this file\n");
      exit(0);
    }
    fgets(b,strlen(b),fp);
    fclose(fp);
       
        strcat(a,b);
    if((fp=fopen("d:\\C.txt","w"))==NULL)
    {
      printf("cannot open this file\n");
      exit(0);
    }
    printf("C is:%s\n",a);
    fputs(a,fp);
    fclose(fp);

        return 0;
}

Jane爱蓝黑墨水 发表于 2014-1-21 13:28:30

牛头的主人 发表于 2014-1-21 12:46 static/image/common/back.gif
//根据你的要求,写了个简短的,你的代码实在看不懂 什么意思,把简单的问题复杂化了
#include
#include ...

能够合并了!!但是还要按字母排列。。。所以我觉得我脑壳转不过来啊。。

牛头的主人 发表于 2014-1-21 15:47:22

Jane爱蓝黑墨水 发表于 2014-1-21 13:28 static/image/common/back.gif
能够合并了!!但是还要按字母排列。。。所以我觉得我脑壳转不过来啊。。

都合并了还不会排列?多动手吧

Jane爱蓝黑墨水 发表于 2014-1-21 16:08:37

牛头的主人 发表于 2014-1-21 15:47 static/image/common/back.gif
都合并了还不会排列?多动手吧

恩恩,谢啦!!

牛头的主人 发表于 2014-1-21 16:48:50

Jane爱蓝黑墨水 发表于 2014-1-21 16:08 static/image/common/back.gif
恩恩,谢啦!!

实在不会我给你程序

SLT 发表于 2014-1-21 17:27:08

过来凑个热闹。。。

Jane爱蓝黑墨水 发表于 2014-1-21 17:50:59

牛头的主人 发表于 2014-1-21 16:48 static/image/common/back.gif
实在不会我给你程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
    FILE *fp;
    char a;
    char b;
    char c;
    char ch;
    int i=0,n;

    if((fp=fopen("d:\\A.txt","r"))==NULL)
    {
      printf("cannot open this file\n");
      exit(0);
    }
    fgets(a,strlen(a),fp);

    fclose(fp);

    if((fp=fopen("d:\\B.txt","r"))==NULL)
    {
      printf("cannot open this file\n");
      exit(0);
    }
    fgets(b,strlen(b),fp);
    fclose(fp);



    if((fp=fopen("d:\\C.txt","w"))==NULL)
    {
      printf("cannot open this file\n");
      exit(0);
    }
    printf("C is:%s\n",c);
    fputs(c,fp);
    fclose(fp);

    return 0;

    for(;ch=fgetc(fp)!=EOF;i++)
    {
      a=ch;
      putchar(a);
    }
    for(;ch=fgetc(fp)!=EOF;i++)
    {
      b=ch;
      putchar(b);
    }
    int j;
    char temp;
    for(i=0;i<n-1;i++)
    {
      for(j=i+1;j<n;j++)
      {
            if(c<c)
            {
                temp=c;
                c=c;
                c=temp;
            }

      }
    }
}

这是我试了N次没报错但是运行起来不对的代码。。。我感觉莫得问题了。。最后运行出来是一个问号。。。



牛头的主人 发表于 2014-1-21 19:24:30

Jane爱蓝黑墨水 发表于 2014-1-21 17:50 static/image/common/back.gif
#include
#include
#include


还有什么问题加我QQ吧,免得一直上论坛,1047564769

Jane爱蓝黑墨水 发表于 2014-1-21 20:36:59

牛头的主人 发表于 2014-1-21 19:24 static/image/common/back.gif
还有什么问题加我QQ吧,免得一直上论坛,1047564769

{:2_31:}谢谢啊!!
页: [1]
查看完整版本: 又来寻找大神了、、、、、