File Systems Files and directories Absolute and relative names Text and binary files Sequential-access and random-access files
File System Input and Output One time access vs over-a-period-of- time access One time: call a static member function Over-a-period-of- time: create an instance of a class Classes for Files File FileInfo Classes for Directories Directory DirectoryInfo
File and FileInfo in C++/CLI File::Exists(path) FileInfo^ file = gcnew FileInfo( path ); file->Exists file->FullName file->Length file->Directory file->isReadOnly file->CopyTo( path2 ) us/library/system.io.file.aspx us/library/system.io.fileinfo.aspx
Directory and DirectoryInfo in C++/CLI Directory::Exists(path) DirectoryInfo^ dir=gcnew DirectoryInfo(path); dir->Exists dir->FullName dir->Parent array ^ GetFiles() array ^ GetDirectories() us/library/system.io.directory.aspx us/library/system.io.directoryinfo.aspx
Opening Files and I/O Open, Create, OpenRead, etc. Open Methods –Mode: Append, Create, CreateNew, Open, etc –Access: Read, Write, ReadWrite –Share: None, Read, Write, ReadWrite Stream, FileStream, StreamWriter, StreamReader
Sequential-access Text File in C++/CLI StreamReader ^sr = File::OpenText(name); // or: StreamReader ^sr = gcnew StreamReader(name); sr->Read();// read one char sr->ReadLine();// read one line sr->Close() StreamWriter ^sw = gcnew StreamWriter(name); sw->Write(“string”); sw->WriteLine(“string”); sw->Close()
Object Serialization in C++/CLI Dealing with complex classes [Serializable] ref class MyObject {} BinaryFormatter: using namespace System::Runtime::Serialization::Formatters::Binary; SoapFormatter: // Simple Objet Access Protocol, XML #using using namespace System::Runtime::Serialization::Formatters::Soap;
Object Serialization in C++/CLI cont’d Serialization steps: BinaryFormatter ^bf = gcnew BinaryFormatter() // SoapFormatter, XML-based FileStream output = File::Create(name); bf->Serialize(output, obj); // Store MyObject obj; output.close(); BinaryFormatter ^bf = gcnew BinaryFormatter() FileStream input = File::OpenRead(name); MyObject^ obj = (MyObject^)bf->Deserialize(input); //Restore MyObject obj input.close();
An Example … Also, Command line parameters: “Project” -> “Properties” -> “Configuration Properties” -> “Debugging” –On that page, find “Command Arguments”, add the file name
Common File Dialog Boxes OpenFileDialog us/library/system.windows.forms.openfiledi alog.aspxhttp://msdn.microsoft.com/en- us/library/system.windows.forms.openfiledi alog.aspx SaveFileDialog us/library/system.windows.forms.savefiledi alog.aspxhttp://msdn.microsoft.com/en- us/library/system.windows.forms.savefiledi alog.aspx