| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 314 人关注过本帖
标题:关于文件的保存问题。。。。。。。
只看楼主 加入收藏
wanglf
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-9-10
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:4 
关于文件的保存问题。。。。。。。
# include <stdio.h>
 # include <string.h>
 # include <iostream.h>
 # include <fstream>

 #define LIMIT 80
 void jia(char *ch1,char *ch2,char *ch3)
 {
      int len1=strlen(ch1),len2=strlen(ch2),len3,i,tmp,num1[LIMIT]={0},num2[LIMIT]={0},num3[LIMIT]={0};
      for(i=len1-1;i>=0;i--)
          num1[len1-1-i]=ch1[i]-48;
      for(i=len2-1;i>=0;i--)
          num2[len2-1-i]=ch2[i]-48;
      i=0,tmp=0,len3=len1>len2?len1:len2;
      while(i<len3)
      {
      num3[i]=num1[i]+num2[i]+tmp;
      tmp=0;
      if(num3[i]>=10)
      {tmp=num3[i]/10;
       num3[i]%=10;}
      i++;
      }
      if(tmp!=0)
      {num3[i]=tmp;i++;}
      len3=i;
      for(i=len3-1;i>=0;i--) ch3[len3-1-i]=num3[i]+48;
      ch3[len3]='\0';

 }
      void cheng(char *ch1,char *ch2,char *ch3)
      {
          int i,j,len1=strlen(ch1),len2=strlen(ch2),len3,
                  temp,num1[LIMIT]={0},num2[LIMIT]={0},num3[LIMIT]={0};
          for(i=len1-1;i>=0;i--)
              num1[len1-1-i]=ch1[i]-48;
          for(i=len2-1;i>=0;i--)
              num2[len2-1-i]=ch2[i]-48;
          for(i=0;i<len1;i++)
          {
              temp=0;
          for(j=0;j<len2;j++)
              {
                num3[i+j]+=num1[i]*num2[j]+temp;
                temp=0;
                if(num3[i+j]>=10)
                {
                    temp=num3[i+j]/10;
                    num3[i+j]%=10;
                }
              }
          while(temp!=0)
              {
                  num3[i+j]+=temp;temp=num3[i+j]/10;j++;
              }
         
          }
          len3=len1+len2;
          while(len3>1&&num3[len3-1]==0)
              len3--;
          for(i=0;i<len3;i++)
              ch3[len3-1-i]=num3[i]+48;
          ch3[len3]='\0';

     }
      
      void main()
       {
          cout<<"                        *********主菜单*********\n";
          cout<<"                        *       1 两数相加     *\n";
          cout<<"                        *       2 两数相乘     *\n";
          cout<<"                        *       3 退出         *\n";
          cout<<"                        ************************\n";
           char ch1[LIMIT],ch2[LIMIT],ch3[LIMIT];
           void jia(char *ch1,char *ch2,char *ch3);
           void cheng(char *ch1,char *ch2,char *ch3);
           int a=0;
           cin>>a;
           switch(a)
           {
           case 1:
               {
                   printf("请输入第一个数\n");
                   scanf("%s",ch1);
                    printf("请输入第二个数\n");
                    scanf("%s",ch2);
                     printf("\n");
                     jia(ch1,ch2,ch3);
                    printf("两数之和:%s\n",ch3);
                    break;
               }
           case 2:
               {
                    printf("请输入第一个数\n");
                    scanf("%s",ch1);
                    printf("请输入第二个数\n");
                    scanf("%s",ch2);
                    printf("\n");
                    cheng(ch1,ch2,ch3);
                    printf("两数之积为:%s\n",ch3);
                    break;
               }
         
               
           case 3:
               {
                   cout<<"退出";
                   break;
               }
           default:
               {
                   cout<<"输入错误";
                   break;
               }
           }
     
}
这个程序要怎么加代码才能实现把运行结果保存在TXT文件中呢?并且保存形式类似这样“123456+123456=246912”这样,请哪位大虾解答一下,谢谢了。
搜索更多相关主题的帖子: void include 
2012-09-12 10:13
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:0 
这个比较简单吧
仅仅是打开文件,写入字符串而已。
2012-09-12 10:30
wanglf
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-9-10
收藏
得分:0 
回复 2楼 silent_world
但是具体应该是怎样的呢?我们老师都还没教过这个,是课程设计里面有这个。还有就是刚才没写清楚,就是不光是要保存还要读入,比如说在主菜单里加入一项“从文件操作”,然后有一个txt文档里面有几个长整数,怎么才能读入和象上面那样保存结果呢?
2012-09-12 10:37
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:10 
呵呵,给一段代码你参考。
int saveFile(char *databuf, int len)
{
FILE *fp;

if( (fp=fopen("111.txt", "w")) == NULL )
     {
         printf("Cannot open w file\n");
         return 0;
     }

fwrite(databuf, 1, len, fp);

fclose(fp);

}

这仅仅是教科书代码,需要你自己处理不同的数据,数据循环。

2012-09-12 11:26
wanglf
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-9-10
收藏
得分:0 
回复 4楼 silent_world
谢谢啊!写出来了,可为什么保存的文件里面什么都没有呢?
# include <stdio.h>
 # include <string.h>
 # include <iostream.h>
 # include <fstream>
#include<stdlib.h>

 #define LIMIT 80

void jia(char *ch1,char *ch2,char *ch3)
 {
      int len1=strlen(ch1),len2=strlen(ch2),len3,i,tmp,num1[LIMIT]={0},num2[LIMIT]={0},num3[LIMIT]={0};
      for(i=len1-1;i>=0;i--)
          num1[len1-1-i]=ch1[i]-48;
      for(i=len2-1;i>=0;i--)
          num2[len2-1-i]=ch2[i]-48;
      i=0,tmp=0,len3=len1>len2?len1:len2;
      while(i<len3)
      {
      num3[i]=num1[i]+num2[i]+tmp;
      tmp=0;
      if(num3[i]>=10)
      {tmp=num3[i]/10;
       num3[i]%=10;}
      i++;
      }
      if(tmp!=0)
      {num3[i]=tmp;i++;}
      len3=i;
      for(i=len3-1;i>=0;i--) ch3[len3-1-i]=num3[i]+48;
      ch3[len3]='\0';

 }
      void cheng(char *ch1,char *ch2,char *ch3)
      {
          int i,j,len1=strlen(ch1),len2=strlen(ch2),len3,
                  temp,num1[LIMIT]={0},num2[LIMIT]={0},num3[LIMIT]={0};
          for(i=len1-1;i>=0;i--)
              num1[len1-1-i]=ch1[i]-48;
          for(i=len2-1;i>=0;i--)
              num2[len2-1-i]=ch2[i]-48;
          for(i=0;i<len1;i++)
          {
              temp=0;
          for(j=0;j<len2;j++)
              {
                num3[i+j]+=num1[i]*num2[j]+temp;
                temp=0;
                if(num3[i+j]>=10)
                {
                    temp=num3[i+j]/10;
                    num3[i+j]%=10;
                }
              }
          while(temp!=0)
              {
                  num3[i+j]+=temp;temp=num3[i+j]/10;j++;
              }
         
          }
          len3=len1+len2;
          while(len3>1&&num3[len3-1]==0)
              len3--;
          for(i=0;i<len3;i++)
              ch3[len3-1-i]=num3[i]+48;
          ch3[len3]='\0';

     }
      
      void main()
       {
          cout<<"                        *********主菜单*********\n";
          cout<<"                        *       1 两数相加     *\n";
          cout<<"                        *       2 两数相乘     *\n";
          cout<<"                        *       3 退出         *\n";
          cout<<"                        ************************\n";
           char ch1[LIMIT],ch2[LIMIT],ch3[LIMIT];
           void jia(char *ch1,char *ch2,char *ch3);
           void cheng(char *ch1,char *ch2,char *ch3);

           int  save(char ch3[]);
           int a=0;
           cin>>a;
         
           switch(a)
           {
           case 1:
               {
                   printf("请输入第一个数\n");
                   scanf("%s",ch1);
                    printf("请输入第二个数\n");
                    scanf("%s",ch2);
                     printf("\n");
                     jia(ch1,ch2,ch3);
                    printf("两数之和:%s\n",ch3);
                     save(ch3);break;
               }
           case 2:
               {
                    printf("请输入第一个数\n");
                    scanf("%s",ch1);
                    printf("请输入第二个数\n");
                    scanf("%s",ch2);
                    printf("\n");
                    cheng(ch1,ch2,ch3);
                    printf("两数之积为:%s\n",ch3);
                     save(ch3);break;
               }
   
         
               
           case 3:
               {
                   cout<<"退出";
                   save(ch3);
                   exit(0);
                   break;
               }
           default:
               {
                   cout<<"输入错误";
                   break;
               }
           }
     
}


      int  save(char ch3[])
{     
      int i;
      FILE *fp;
     if((fp=fopen("data.txt","rb"))==NULL)
     {
       printf("出现错误,请检查文件是否存在!!");
       getchar();
       return 0;
     }
     for(i=0;i<LIMIT;i++)
     {
       fputs(&ch3[i],fp);
     }
     fclose(fp);
     return 0;
}
2012-09-12 17:37
快速回复:关于文件的保存问题。。。。。。。
数据加载中...
 
   



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

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