您的位置:首页 > 编程语言

手动创建两个文本文件text1.txt,text2.txt,要求编程创建text3.txt,实现text1.txt和text2.txt文件中除去首行和末尾,其余对应的数据相加,三个文本的内容如下

2013-08-06 00:45 1306 查看
text1.txt
begin
10 11 12
20 21 22
30 31 32
end

text2.txt
begin
15 16 17
25 26 27
35 36 37
end

text3.txt
begin
25 27 29
45 47 49
65 67 69
end

1.0版。。。有待完善

#include<stdio.h>
int main()
{
   FILE *file_from1,*file_to,*file_from2;
   char date,date_another;               
   int str;
   int num;
   int count;                
   int total;              
   char str_c;            
   char count_c;          
   file_from1 = fopen("wenjian.txt","r");
   if(NULL == file_from1)
   {
       perror("open wenjian.txt");
       return -1;  
   }
   file_from2 = fopen("wenjian1.txt","r");
   if(NULL == file_from2)
   {
       perror("open wenjian1.txt");
       return -1;  
   }
   file_to = fopen("wenjian2.txt","w");
   if(NULL == file_to)
   {
       perror("open wenjian2.txt");
       return -1;
   }
   while(1)
   {                    
       total = 0;   
       date = fgetc(file_from1);
       date_another = fgetc(file_from2);
       if(date <= '9' && date >= '0')
       {
           while(date <= '9'&& date >= '0')
    {
          num = date - '0' + date_another - '0';

                total =total * 10 + num;       
                date = fgetc(file_from1);
         date_another = fgetc(file_from2);
    }
    str = total / 10;
           count = total % 10;
    str_c = str + '0';         fputc(str_c,file_to);
    fputc(count_c,file_to);  
   }       
   if(EOF == date)       
   {
        break;
   }
   fputc(date,file_to);          return 0;
}

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐