注册 登录
编程论坛 C++教室

关于文件写入的问题

qq1274371820 发布于 2014-06-16 22:07, 555 次点击

              怎么把下面的数据写入文件中?

              ORDER 是我定义的class ORDER

    ORDER order[10]={
    ORDER("flat shoes",1,20,400),
    ORDER("leather shoes",2,60,200),
    ORDER("military shoes",3,15,52),
    ORDER("footware shoes",4,25,600),
    ORDER("sports shoes",5,90,100),
    ORDER("travel shoes",6,80,200),
    ORDER("canvas shoes",7,75,150),
    ORDER("high heeled ",8,88,340)};
2 回复
#2
qq12743718202014-06-17 13:12


              额,我说仔细一点好了。
      
               就是下面这个代码,但是有错,要怎么改才能把O[9]里面的数据存入到chakan.txt文件中?




#include "stdafx.h"
#include"iostream"
#include"fstream"
#include"string"
#include<conio.h>  
using namespace std;
class ORDER
{
private:

    string PRONAME;
    int ID;
    float PRICE;
    int NUMBER;
public:
    ORDER(){};
    void chakan();
};
void ORDER::chakan()
{
    ORDER O[9];
    O[0].setdingdan("flat shoes",1,20,400);
    O[1].setdingdan("leather shoes",2,60,200);
    O[2].setdingdan("military shoes",3,15,52);
    O[3].setdingdan("footware shoes",4,25,600);
    O[4].setdingdan("sports shoes",5,90,100);
    O[5].setdingdan("travel shoes",6,80,200);
    O[6].setdingdan("canvas shoes",7,75,150);
    O[7].setdingdan("high heeled ",8,88,340);
    ofstream outfile;
    outfile.open("g:\\chakan.txt",ios::out);
    if(!outfile)
    {
        cerr<<"文件打开失败!"<<endl;
        exit(0);
    }
    for(int i=0;i<8;i++)
    {
        outfile<<O[i]<<endl;
    }
    outfile.close();
}
int main()
{
    ORDER j[8];
    ifstream infile;
    infile.open("g:\\chakan.txt",ios::in);
    if(!infile)
    {
         cerr<<"文件打开失败!"<<endl;
         exit(0);
    }
    for(int i=0;i<8;i++)
    {
           infile>>j[i];
           cout<<j[i]<<endl;
    }
    infile.close();
    return 0;
}

#3
qq12743718202014-06-17 13:54


               噢,public里面还有个函数:

           void ORDER::setdingdan(string proname,int id,float price,int number)
    {
        PRONAME=proname;
        ID=id;
        PRICE=price;
        NUMBER=number;
    }
1