File Systems Files and directories Absolute and relative
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. Info Classes for Directories Directory. Info
File and File. Info in C++/CLI • File: : Exists(path) • • • File. Info^ file = gcnew File. Info( path ); file->Exists file->Full. Name file->Length file->Directory file->is. Read. Only • http: //msdn. microsoft. com/enus/library/system. io. file. aspx • http: //msdn. microsoft. com/enus/library/system. io. fileinfo. aspx
Directory and Directory. Info in C++/CLI • Directory: : Exists(path) • • • Directory. Info^ dir=gcnew Directory. Info(path); dir->Exists dir->Full. Name dir->Parent array<File. Info^>^ Get. Files() array<Directory. Info^>^ Get. Directories() • http: //msdn. microsoft. com/enus/library/system. io. directory. aspx • http: //msdn. microsoft. com/enus/library/system. io. directoryinfo. aspx
Opening Files and I/O • Open, Create, Open. Read, etc. • Open Methods – Mode: Append, Create. New, Open, etc – Access: Read, Write, Read. Write – Share: None, Read, Write, Read. Write • Stream, File. Stream, Stream. Writer, Stream. Reader
Sequential-access Text File in C++/CLI • Stream. Reader ^sr = File: : Open. Text(name); // or: • • Stream. Reader ^sr = gcnew Stream. Reader(name); sr->Read(); // read one char sr->Read. Line(); // read one line sr->Close() • • Stream. Writer ^sw = gcnew Stream. Writer(name); sw->Write(“string”); sw->Write. Line(“string”); sw->Close()
Object Serialization in C++/CLI • Dealing with complex classes • [Serializable] ref class My. Object {} • Binary. Formatter ^bf = gcnew Binary. Formatter() // Soap. Formatter, XML-based • File. Stream output = File: : Create(name); • bf->Serialize(output, obj); // Store My. Object obj; • output. close(); • Binary. Formatter ^bf = gcnew Binary. Formatter() • File. Stream input = File: : Open. Read(name); • My. Object^ obj = (My. Object^)bf->Deserialize(input); //Restore My. Object obj • input. close();
An Example
- Slides: 8