鱼C论坛

 找回密码
 立即注册
查看: 3422|回复: 5

如何将C++程序中产生的数据写到文件里保存下来

[复制链接]
发表于 2011-6-27 02:01:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我是个新手。最近正在做课程设计,老师要求把程序中产生的数据,写到文件里,但我实在不知道咋整了。。。求大家帮忙了。谢谢
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-6-27 08:31:43 | 显示全部楼层
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;

  4. void main()
  5. {
  6.     ofstream in;
  7. in.open("com.txt",ios::trunc); //ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建
  8. int i;
  9. char a='a';
  10. for(i=1;i<=26;i++)//将26个数字及英文字母写入文件
  11. {
  12.    if(i<10)
  13.    {
  14.     in<<"0"<<i<<"\t"<<a<<"\n";
  15.     a++;
  16.    }
  17.    else
  18.    {
  19.     in<<i<<"\t"<<a<<"\n";
  20.     a++;
  21.    }
  22. }
  23. in.close();//关闭文件
  24. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-6-27 09:25:33 | 显示全部楼层
按楼上的做法,LZ要改很多代码,LZ也可以试试我的做法。
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;

  4. int main(){
  5.         //这两行将cout和文件data.txt连接
  6.         ofstream out("data.txt");
  7.         streambuf* old_buffer=cout.rdbuf(out.rdbuf());       
  8.         //所有cout的东西都会输出到文件data.txt
  9.         cout<<"Hello"<<endl;
  10.         cout<<"again"<<endl;
  11.         //恢复cout
  12.         cout.rdbuf(old_buffer);
  13. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-6-28 16:18:17 | 显示全部楼层

我这个写了个停车场管理系统的程序,想把程序中输入的char car_type; //汽车类型   int number; //汽车牌号  int ar_time; //汽车到达时间还有int time_d;//汽车离开时间      int money;//汽车停车费写到文件里,但实在改不过来了,请您帮个忙,改下我的程序,把我刚刚说的能写到文件里,谢谢了

  1. #include<stdio.h>//定义输入、输出函数
  2. #include<iostream>//数据流输入、输出
  3. #include<string.h>//字符串处理
  4. #include<math.h>//定义数学函数
  5. #include<stdlib.h>//定义杂项函数
  6. using namespace std;
  7. #define size 3 //停车场位置数
  8. typedef struct car_information  {
  9.   char car_type; //汽车类型
  10.      int number; //汽车车号
  11.      int ar_time; //汽车到达时间
  12. }Scar_information;
  13. typedef struct{
  14.      Scar_information    *base;  //停车场的栈底
  15.      Scar_information    *top;  //停车场的栈顶
  16.      int stacksize;
  17. // int car_type;
  18. }stackhead;
  19. void initstack(stackhead &L)  //构造一个空栈
  20. {
  21.   L.base=(Scar_information*)malloc(size*sizeof(Scar_information));
  22.   if(!L.base)exit(0); //存储分配失败
  23.   L.top=L.base;
  24.   L.stacksize=size;
  25. }
  26. void push(stackhead &L,Scar_information e) //把元素e压入L栈成为新的栈顶元素
  27. {
  28.      *L.top=e;
  29.   L.top++;
  30.     // L.stacksize++;
  31. }
  32. void pop(stackhead &L,Scar_information &e)  //把元素e弹出L栈
  33. {
  34.    if(L.top==L.base)
  35.    {
  36.        cout<<"停车场没有车辆!!"<<endl;
  37.        return;
  38.    }
  39.    L.top-- ;
  40.    e=*L.top;
  41. //  L.stacksize--;
  42. }
  43. //便道队列的性质
  44. typedef struct vehicle_information{
  45.    char car_type; //汽车类型
  46.    int number;  //汽车车牌号
  47.    int ar_time;  //汽车到达时间
  48.    struct vehicle_information *next;
  49. }*Qvehicle_information;
  50. typedef struct{
  51.    Qvehicle_information front;  //便道队列的队头
  52.    Qvehicle_information rear;  //便道队列的队尾
  53.    int length;
  54. }linkqueue;
  55. //队列的基本操作
  56. void initqueue(linkqueue &q)//构造一个空队列
  57. {
  58.    q.front=q.rear=(Qvehicle_information)malloc(sizeof(vehicle_information));
  59.    if(!q.front||!q.rear)
  60.          exit(0);
  61.    q.front->next=NULL;
  62.    q.length=0;
  63. }
  64. void enqueue(linkqueue &q,int number,int ar_time,int car_type)
  65. {
  66.     Qvehicle_information p;
  67.     p=(Qvehicle_information)malloc(sizeof(vehicle_information));
  68.     if(!p)exit(0);
  69. p->car_type;
  70.     p->number=number;
  71.     p->ar_time=ar_time;
  72.     p->next=NULL;
  73.     q.rear->next=p;
  74.     q.rear=p;
  75.     q.length++;
  76. }
  77. void popqueue(linkqueue &q,Qvehicle_information &w)
  78. //删除队列中的车辆
  79. {
  80.     Qvehicle_information p;
  81.     if(q.front==q.rear)
  82.     {
  83.          cout<<"停车场的便道没有车辆!!"<<endl;
  84.          return;
  85.     }
  86.     p=q.front->next;
  87.     w=p;
  88.     q.front->next=p->next;
  89.     q.length--;
  90.     if(q.rear==p)q.front=q.rear;
  91. }
  92. Scar_information enter_park(stackhead &st,linkqueue &q)  //对进入停车场的汽车的处理
  93. {
  94.      int number, time_a;
  95.   char car_type;
  96.   Scar_information e;
  97.   cout<<"汽车型号:";
  98.   cin>>car_type;
  99.      cout<<"车牌号为:";
  100.      cin>>number;
  101.      cout<<"进入停车场的时刻:";
  102.      cin>>time_a;
  103.      if(st.top-st.base<st.stacksize){
  104.    e.car_type=car_type;
  105.          e.number=number;
  106.          e.ar_time=time_a;
  107.          push(st,e);
  108.          cout<<"该车已经进入停车场在:"<<st.top-st.base<<"号车道"<<endl<<endl;
  109.      }
  110.      else{
  111.          enqueue(q,number,time_a,car_type);//插入元素time_a为q的新的队尾元素
  112.          cout<<"停车场已满,该车先停在便道的第"<<q.length<<"个位置上"<<endl;
  113.          }
  114.   return e;
  115. }
  116. void leave_park(stackhead &st,stackhead &sl,linkqueue &q, Scar_information e[])//对离开停车场的汽车的处理
  117. {//st堆栈为停车场,sl堆栈为临时的停车场
  118.      int number,time_d,flag=1,money,arrivaltime,unit_price;char car_type;//q为便道队列
  119.   int i, t;
  120.   Scar_information q_to_s;
  121.   cout<<"汽车车牌为:";
  122.      cin>>number;
  123.   for(i=0; i<3; ++i)
  124.     if(e[i].number==number)
  125.        {
  126.    cout<<"该车类型为:"<<e[i].car_type<<"请根据汽车类型输入停车费单价:";
  127.    t=i;
  128.        }
  129.      cout<<"停车费单价:";
  130.   cin>>unit_price;
  131.      cout<<"离开停车场时刻:";
  132.      cin>>time_d;
  133.      Qvehicle_information w;
  134.   
  135.      while(flag){//找到要开出的车,并且弹出停车场栈
  136.          pop(st,e[t]);
  137.          push(sl,e[t]);
  138.          if(e[t].number==number)
  139.          {
  140.           flag=0;
  141.           money=(time_d-e[t].ar_time)*unit_price;
  142.           arrivaltime=e[t].ar_time;
  143.          }
  144.      }
  145.      pop(sl,e[i]);//把临时停车场的第一辆车(要离开的)去掉//这里的错误原因在于i的值为3,超出e数组原来的范围
  146.      while(sl.top!=sl.base)//把临时停车场的车倒回停车场
  147.      {
  148.          pop(sl,e[t]);
  149.          push(st,e[t]);
  150.      }
  151.      if(sl.top-sl.base<st.stacksize&&q.length!=0)//停车场有空位置,便道上的车开进停车场
  152.      {
  153.         popqueue(q,w);
  154.   q_to_s.car_type=car_type;
  155.         q_to_s.ar_time=time_d;
  156.         q_to_s.number=w->number;
  157.         push(st,q_to_s);
  158.         cout<<"车牌号为"<<q_to_s.number<<"的车已经从便道进入停车场,所在停车位为:"<<st.top-st.base<<endl<<endl;
  159.      }
  160.      cout<<"              收       据"<<endl;
  161.      cout<<"       ========================= 车牌号:"<<number<<endl;
  162. cout<<"===================================================="<<endl;
  163.      cout<<"|进停车场时刻|出停车场时刻|停留时间|应付(元)|"<<endl;
  164. cout<<"===================================================="<<endl;
  165.      cout<<"|         "<<arrivaltime<<"  |       "<<time_d<<"    |     "<<time_d-arrivaltime<<"    |      "<<money<<"     |"<<endl;
  166.      cout<<"--------------------------------------"<<endl<<endl;
  167.     }
  168.     int main()
  169.     {

  170.        int m=100;
  171.     int i=0;
  172.        char flag;//进入或者离开停车场的标识
  173.     char order;
  174.     //char Y;
  175.        stackhead sting,slinshi;  //停车场和临时停车场栈的定义
  176.     Scar_information e[200];    //添加一个数组存放三辆车信息,尽可能讲数组的大小扩大,因为其存放的是车辆信息,程序可以重复100次,所以数组值相应扩大
  177.        linkqueue   line;         //队列的定义
  178.        initstack(sting);         //构造停车场栈sting
  179.        initstack(slinshi);       //构造临时停车场栈slinshi
  180.        initqueue(line);          //构造便道队列line
  181.        while(m)
  182.        {
  183.         
  184.        cout<<"                      欢迎使用"<<endl;
  185.          cout<<"\n                 **停车场管理系统**"<<endl;
  186.          cout<<"======================================================"<<endl;
  187.          cout<<"**                                                **"<<endl;
  188.          cout<<"**       A--汽车进入停车场  D--汽车离开停车场     **"<<endl;
  189.          cout<<"**                                                **"<<endl;
  190.          cout<<"**                   E--退出 程序                 **"<<endl;
  191.    cout<<"**             附:汽车类型及停车费单价           **"<<endl;
  192.    cout<<"**      货车T,10元--客车B,8元--小型车C,4元     **"<<endl;
  193.          cout<<"======================================================"<<endl;
  194.    cout<<"是否需要清屏?(Y/N)";
  195.    cin>>order;
  196.    if('Y'==order)
  197.    {
  198.     system ("cls");
  199.    }
  200.          cout<<"请选择:(A,D,E):";
  201.          cin>>flag;
  202.          switch(flag)
  203.          {
  204.            case'A':e[i]=enter_park(sting,line);i++;break;         //汽车进入停车场
  205.            case'D':leave_park(sting,slinshi,line,e);break; //汽车离开停车场
  206.            case'E':exit(0);
  207.    }
  208.          m--;
  209.    
  210.        }
  211.      
  212.    
  213.       return 0;
  214.      
  215.    }              
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-6-28 16:18:54 | 显示全部楼层
仰望天上的光 发表于 2011-6-27 09:25
按楼上的做法,LZ要改很多代码,LZ也可以试试我的做法。

我这个写了个停车场管理系统的程序,想把程序中输入的char car_type; //汽车类型 int number; //汽车牌号 int ar_time; //汽车到达时间还有int time_d;//汽车离开时间 int money;//汽车停车费写到文件里,但实在改不过来了,请您帮个忙,改下我的程序,把我刚刚说的能写到文件里,谢谢了

  1. // 停车场管理.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include<stdio.h>//定义输入、输出函数
  5. #include<iostream>//数据流输入、输出
  6. #include<string.h>//字符串处理
  7. #include<math.h>//定义数学函数
  8. #include<stdlib.h>//定义杂项函数
  9. using namespace std;
  10. #define size 3 //停车场位置数
  11. typedef struct car_information  {
  12.   char car_type; //汽车类型
  13.      int number; //汽车车号
  14.      int ar_time; //汽车到达时间
  15. }Scar_information;
  16. typedef struct{
  17.      Scar_information    *base;  //停车场的栈底
  18.      Scar_information    *top;  //停车场的栈顶
  19.      int stacksize;
  20. // int car_type;
  21. }stackhead;
  22. void initstack(stackhead &L)  //构造一个空栈
  23. {
  24.   L.base=(Scar_information*)malloc(size*sizeof(Scar_information));
  25.   if(!L.base)exit(0); //存储分配失败
  26.   L.top=L.base;
  27.   L.stacksize=size;
  28. }
  29. void push(stackhead &L,Scar_information e) //把元素e压入L栈成为新的栈顶元素
  30. {
  31.      *L.top=e;
  32.   L.top++;
  33.     // L.stacksize++;
  34. }
  35. void pop(stackhead &L,Scar_information &e)  //把元素e弹出L栈
  36. {
  37.    if(L.top==L.base)
  38.    {
  39.        cout<<"停车场没有车辆!!"<<endl;
  40.        return;
  41.    }
  42.    L.top-- ;
  43.    e=*L.top;
  44. //  L.stacksize--;
  45. }
  46. //便道队列的性质
  47. typedef struct vehicle_information{
  48.    char car_type; //汽车类型
  49.    int number;  //汽车车牌号
  50.    int ar_time;  //汽车到达时间
  51.    struct vehicle_information *next;
  52. }*Qvehicle_information;
  53. typedef struct{
  54.    Qvehicle_information front;  //便道队列的队头
  55.    Qvehicle_information rear;  //便道队列的队尾
  56.    int length;
  57. }linkqueue;
  58. //队列的基本操作
  59. void initqueue(linkqueue &q)//构造一个空队列
  60. {
  61.    q.front=q.rear=(Qvehicle_information)malloc(sizeof(vehicle_information));
  62.    if(!q.front||!q.rear)
  63.          exit(0);
  64.    q.front->next=NULL;
  65.    q.length=0;
  66. }
  67. void enqueue(linkqueue &q,int number,int ar_time,int car_type)
  68. {
  69.     Qvehicle_information p;
  70.     p=(Qvehicle_information)malloc(sizeof(vehicle_information));
  71.     if(!p)exit(0);
  72. p->car_type;
  73.     p->number=number;
  74.     p->ar_time=ar_time;
  75.     p->next=NULL;
  76.     q.rear->next=p;
  77.     q.rear=p;
  78.     q.length++;
  79. }
  80. void popqueue(linkqueue &q,Qvehicle_information &w)
  81. //删除队列中的车辆
  82. {
  83.     Qvehicle_information p;
  84.     if(q.front==q.rear)
  85.     {
  86.          cout<<"停车场的便道没有车辆!!"<<endl;
  87.          return;
  88.     }
  89.     p=q.front->next;
  90.     w=p;
  91.     q.front->next=p->next;
  92.     q.length--;
  93.     if(q.rear==p)q.front=q.rear;
  94. }
  95. Scar_information enter_park(stackhead &st,linkqueue &q)  //对进入停车场的汽车的处理
  96. {
  97.      int number, time_a;
  98.   char car_type;
  99.   Scar_information e;
  100.   cout<<"汽车型号:";
  101.   cin>>car_type;
  102.      cout<<"车牌号为:";
  103.      cin>>number;
  104.      cout<<"进入停车场的时刻:";
  105.      cin>>time_a;
  106.      if(st.top-st.base<st.stacksize){
  107.    e.car_type=car_type;
  108.          e.number=number;
  109.          e.ar_time=time_a;
  110.          push(st,e);
  111.          cout<<"该车已经进入停车场在:"<<st.top-st.base<<"号车道"<<endl<<endl;
  112.      }
  113.      else{
  114.          enqueue(q,number,time_a,car_type);//插入元素time_a为q的新的队尾元素
  115.          cout<<"停车场已满,该车先停在便道的第"<<q.length<<"个位置上"<<endl;
  116.          }
  117.   return e;
  118. }
  119. void leave_park(stackhead &st,stackhead &sl,linkqueue &q, Scar_information e[])//对离开停车场的汽车的处理
  120. {//st堆栈为停车场,sl堆栈为临时的停车场
  121.      int number,time_d,flag=1,money,arrivaltime,unit_price;char car_type;//q为便道队列
  122.   int i, t;
  123.   Scar_information q_to_s;
  124.   cout<<"汽车车牌为:";
  125.      cin>>number;
  126.   for(i=0; i<3; ++i)
  127.     if(e[i].number==number)
  128.        {
  129.    cout<<"该车类型为:"<<e[i].car_type<<"请根据汽车类型输入停车费单价:";
  130.    t=i;
  131.        }
  132.      cout<<"停车费单价:";
  133.   cin>>unit_price;
  134.      cout<<"离开停车场时刻:";
  135.      cin>>time_d;
  136.      Qvehicle_information w;
  137.   
  138.      while(flag){//找到要开出的车,并且弹出停车场栈
  139.          pop(st,e[t]);
  140.          push(sl,e[t]);
  141.          if(e[t].number==number)
  142.          {
  143.           flag=0;
  144.           money=(time_d-e[t].ar_time)*unit_price;
  145.           arrivaltime=e[t].ar_time;
  146.          }
  147.      }
  148.      pop(sl,e[i]);//把临时停车场的第一辆车(要离开的)去掉//这里的错误原因在于i的值为3,超出e数组原来的范围
  149.      while(sl.top!=sl.base)//把临时停车场的车倒回停车场
  150.      {
  151.          pop(sl,e[t]);
  152.          push(st,e[t]);
  153.      }
  154.      if(sl.top-sl.base<st.stacksize&&q.length!=0)//停车场有空位置,便道上的车开进停车场
  155.      {
  156.         popqueue(q,w);
  157.   q_to_s.car_type=car_type;
  158.         q_to_s.ar_time=time_d;
  159.         q_to_s.number=w->number;
  160.         push(st,q_to_s);
  161.         cout<<"车牌号为"<<q_to_s.number<<"的车已经从便道进入停车场,所在停车位为:"<<st.top-st.base<<endl<<endl;
  162.      }
  163.      cout<<"              收       据"<<endl;
  164.      cout<<"       ========================= 车牌号:"<<number<<endl;
  165. cout<<"===================================================="<<endl;
  166.      cout<<"|进停车场时刻|出停车场时刻|停留时间|应付(元)|"<<endl;
  167. cout<<"===================================================="<<endl;
  168.      cout<<"|         "<<arrivaltime<<"  |       "<<time_d<<"    |     "<<time_d-arrivaltime<<"    |      "<<money<<"     |"<<endl;
  169.      cout<<"--------------------------------------"<<endl<<endl;
  170.     }
  171.     int main()
  172.     {

  173.        int m=100;
  174.     int i=0;
  175.        char flag;//进入或者离开停车场的标识
  176.     char order;
  177.     //char Y;
  178.        stackhead sting,slinshi;  //停车场和临时停车场栈的定义
  179.     Scar_information e[200];    //添加一个数组存放三辆车信息,尽可能讲数组的大小扩大,因为其存放的是车辆信息,程序可以重复100次,所以数组值相应扩大
  180.        linkqueue   line;         //队列的定义
  181.        initstack(sting);         //构造停车场栈sting
  182.        initstack(slinshi);       //构造临时停车场栈slinshi
  183.        initqueue(line);          //构造便道队列line
  184.        while(m)
  185.        {
  186.         
  187.        cout<<"                      欢迎使用"<<endl;
  188.          cout<<"\n                 **停车场管理系统**"<<endl;
  189.          cout<<"======================================================"<<endl;
  190.          cout<<"**                                                **"<<endl;
  191.          cout<<"**       A--汽车进入停车场  D--汽车离开停车场     **"<<endl;
  192.          cout<<"**                                                **"<<endl;
  193.          cout<<"**                   E--退出 程序                 **"<<endl;
  194.    cout<<"**             附:汽车类型及停车费单价           **"<<endl;
  195.    cout<<"**      货车T,10元--客车B,8元--小型车C,4元     **"<<endl;
  196.          cout<<"======================================================"<<endl;
  197.    cout<<"是否需要清屏?(Y/N)";
  198.    cin>>order;
  199.    if('Y'==order)
  200.    {
  201.     system ("cls");
  202.    }
  203.          cout<<"请选择:(A,D,E):";
  204.          cin>>flag;
  205.          switch(flag)
  206.          {
  207.            case'A':e[i]=enter_park(sting,line);i++;break;         //汽车进入停车场
  208.            case'D':leave_park(sting,slinshi,line,e);break; //汽车离开停车场
  209.            case'E':exit(0);
  210.    }
  211.          m--;
  212.    
  213.        }
  214.      
  215.    
  216.       return 0;
  217.      
  218.    }              
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2014-5-9 12:07:40 | 显示全部楼层
不懂来看看的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 14:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表