CppBinRead.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:2k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- using namespace System;
- using namespace System::IO;
- __gc class Customer
- {
- String* name;
- long accNo;
- double balance;
- public:
- // Constructors
- Customer() : name(0), accNo(0), balance(0.0) {}
- Customer(String* s, long l, double b) :
- name(s), accNo(l), balance(b) {}
- // Write object data to a BinaryWriter
- void Write(BinaryWriter* bw)
- {
- bw->Write(name);
- bw->Write(accNo);
- bw->Write(balance);
- }
- // Read object data from a BinaryReader
- void Read(BinaryReader* br)
- {
- name = br->ReadString();
- accNo = br->ReadInt32();
- balance = br->ReadDouble();
- }
- // Properties to retrieve the instance variables
- __property String* get_Name() { return name; }
- __property long get_Account() { return accNo; }
- __property double get_Balance() { return balance; }
- };
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(int argc, char* argv[])
- #endif
- {
- // Check for required arguments
- if (argc < 2)
- {
- Console::WriteLine(S"Usage: CppBinRead path");
- return -1;
- }
- String* path = new String(argv[1]);
- Customer* c1 = new Customer(S"Fred Smith", 1234567, 100.0);
- Customer* c2 = new Customer(S"Bill Jones", 2345678, 1000.0);
- Customer* c3 = new Customer(S"Dave Davies", 3456789, 5000.0);
- try
- {
- // Create a FileStream
- FileStream* fstrm = new FileStream(path, FileMode::Create,
- FileAccess::ReadWrite);
- // Create a BinaryWriter to use the FileStream
- BinaryWriter* binw = new BinaryWriter(fstrm);
- c1->Write(binw);
- c2->Write(binw);
- c3->Write(binw);
- // Create a BinaryReader that reads from the same FileStream
- BinaryReader* binr = new BinaryReader(fstrm);
- // Move back to the beginning
- binr->BaseStream->Seek(0, SeekOrigin::Begin);
- Customer* c4 = new Customer();
- c4->Read(binr);
- Console::WriteLine("Balance for {0} (a/c {1}) is {2}", c4->Name,
- __box(c4->Account), __box(c4->Balance));
- }
- catch(System::Exception* pe)
- {
- Console::WriteLine(pe->ToString());
- }
- return 0;
- }