// BinaryFileIO_Demo.cpp : main project file. /* Working With BinaryFiles ________________________________________________ Learning C++/CLI Ron Kessler Created 7/28/2016 This little program shows how to use: File IO with the BinaryReader to read/write data from a binary file. Includes structured error handling SEE MURACH PAGES 492-498 After you save data to the file open it in Notepad and notice how numbers are saved. _________________________________________________ */ #include "stdafx.h" using namespace System; using namespace System::IO; //be sure to add this ConsoleKeyInfo cki; //so we can trap keys int Ready2Quit(); //define helper function here void ReadMyFile(); //define helper function here int main(array ^args) { String^ myTitle = "Welcome to Learning About Binary Data Files\n"; String^ showPrompt = "Add inventory data now:\n"; //---define our objects here BinaryWriter^ myWriter; //define binary writer/reader here without instaniating //---print out a nice welcome Console::WriteLine(myTitle); Console::WriteLine(showPrompt); //---get the new data Console::Write("Enter product:"); String^ product = Console::ReadLine(); Console::Write("Enter price:"); double price = Convert::ToDouble(Console::ReadLine()); //---prompt to save Console::WriteLine("\n\nData has been read. Press any key to save to disk....\n"); Console::ReadLine(); try { myWriter = gcnew BinaryWriter(gcnew FileStream("inventory.dat", FileMode::Create, FileAccess::Write)); myWriter->Write(product); myWriter->Write(price); if (myWriter != nullptr) //can't close an object that does not exist! myWriter->Close(); //close here so we can read the file. Can't read a file if it is still open from writing! Console::Write("Your data has been saved.\n\n"); Ready2Quit(); } catch (Exception^ e) { Console::Write("An error occurred while saving....\n" + e->Message); Ready2Quit(); } finally //this block runs when the app is closing! { if (myWriter != nullptr) myWriter->Close(); //make sure to always close the writer } } #pragma region Helper Function int Ready2Quit() { Console::Write("\nPress Escape (Esc) to quit or ENTER to read the file."); cki = Console::ReadKey(); if (cki.Key == ConsoleKey::Escape) return 0; else if (cki.Key == ConsoleKey::Enter) ReadMyFile(); } void ReadMyFile() { Console::Clear(); Console::WriteLine("Here are the items you saved.\n\n"); //---define our objects here BinaryReader^ myReader; //define binary reader here without instaniating String^ myProductFromFile; double myPriceFromFile; try { myReader = gcnew BinaryReader(gcnew FileStream("inventory.dat", FileMode::Open, FileAccess::Read)); while (myReader->PeekChar() != -1) { myProductFromFile = myReader->ReadString(); // item is a string so use this method myPriceFromFile = myReader->ReadDouble(); // item is a double so use this method } Console::Write("Product saved:"); Console::WriteLine(myProductFromFile); Console::Write("Price saved:"); Console::WriteLine(myPriceFromFile.ToString("c")); // use c for currency with $. Use n for no $ //---quit? Ready2Quit(); } catch (Exception^ e) { Console::Write("An error occurred while reading....\n" + e->Message); Ready2Quit(); } finally { if (myReader != nullptr) myReader->Close(); //make sure to always close the reader } } #pragma endregion