Thursday, May 30, 2013

Contoh Program Input Output C++ "menuliskan data structure ke binary file"

/*menuliskan data structure ke binary file */

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct barang {
char nama_barang[20];
int jumlah;
double harga;

};
int main()
{
ofstream fout("Barang.daty", ios::out | ios::binary);
if(!fout)
{
cout<<"File Tidak Bisa di buka.\n";
}
barang brg[3];
strcpy(brg[0].nama_barang,"Sikat Gigi");
brg[0].jumlah=2;
brg[0].harga=10.000;

strcpy(brg[1].nama_barang,"Odol");
brg[1].jumlah=2;
brg[1].harga=10.000;
for(int i=0; i<2; i++)
fout.write((const char *) &brg[i],sizeof(barang));
fout.close();
if(!fout.good()){
cout<<"A file error occurred.";
}
system("PAUSE");
return 0;
}

No comments:

Post a Comment

Link