gaoxian159753 发表于 2017-1-5 19:54:02

文件读取输出

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

#define LEN 40
int main()
{

      FILE *fp,*fd;
      char name_a;
      char name_b;
      char one_name;
      char two_name;
      char Dpos_one;
      char Dpos_two;   
      char *sh = "YOU YOU YOU";
      char *fh = "YOU YOU YOU ?:";
      char ch;
      int i = 0;
      int k;
      int f;
      int j;
      long pos;

      strcat(name_a,sh);
      strcat(name_a,fh);

      k = strlen(name_a);


      puts("Enter the first file name");      //first 第一
      scanf("%s",one_name);
      puts("Enter the second file name");   //second 第二
      scanf("%s",two_name);

      strcat(one_name,".txt");
      strcat(two_name,".txt");

      if((fp = fopen(one_name,"wb+")) == NULL)
      {
                fprintf(stderr,"I couldn't open the file");
                exit(1);
      }

      if((fd = fopen(two_name,"wb+")) == NULL)
      {
                fprintf(stderr,"I couldn't open the file");
                exit(2);
      }

      fwrite(sh,strlen(sh),1,fp);
      fputc('\n',fp);
      fwrite(fh,strlen(fh),1,fp);

      fwrite(sh,strlen(sh),1,fd);
      fputc('\n',fd);
      fwrite(fh,strlen(fh),1,fd);
      
      while(i >= 0 && i < k)
      {
                pos = (long)i * sizeof(char);
                fseek(fp,pos,SEEK_SET);
                fseek(fd,pos,SEEK_SET);
                ch = fgetc(fp);
                if(ch != EOF)
                {
                        printf("%c",ch);            
                }
                i++;
      }
      printf("\n");
      printf("pos = %ld\n",pos);
      printf("\n");
      fclose(fp);
      fclose(fd);
      return 0;
}

我分别写入2个文本
我需要先输出         第1个文本的第1行数据输出 第2文本第1行数据输出
然后在输出        第1个文本的第2行数据输出 第2文本第2行数据输出
在这里遇到难点 我应该如何用偏移量来判断 达到上所需要的要求 输出出来   

xw0314 发表于 2017-1-6 09:59:33

{:5_91:}fgets()直接读取一行不是更好吗

gaoxian159753 发表于 2017-1-6 15:34:33

xw0314 发表于 2017-1-6 09:59
fgets()直接读取一行不是更好吗

对哦忘记了
页: [1]
查看完整版本: 文件读取输出