1 / 18

Introduction to File Structures

Introduction to File Structures. CSC 402 – File Management. Storage. Storage Hierarchy Primary storage Secondary storage Storage of Databases Physical organization Magnetic disks. Files. Logical Physical Access Organization Physical Logical Processing. Processing of a File.

farrah
Télécharger la présentation

Introduction to File Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to File Structures CSC 402 – File Management

  2. Storage • Storage Hierarchy • Primary storage • Secondary storage • Storage of Databases • Physical organization • Magnetic disks

  3. Files • Logical • Physical • Access • Organization • Physical • Logical • Processing

  4. Processing of a File • Create • Open • Close • Read • Write • Append

  5. ios istream ostream iostream ofstream ifstream fstream C++ File Processing • I/O operations are handled by the class ios • iostream and fstream are the header files needed

  6. File Operations • open(filename, mode, [access]); • close(); • read(address of destination, number_bytes); • write(address of source, number_bytes); • seekg(offset, position); • seekp(offset, position); • long tellg(); • long tellp();

  7. Open Modes • ios::app Opens for appending, not updating a specific record • ios::in Opens for reading • ios::out Opens for writing • ios::binary Opens in binary mode

  8. State Functions • eof() Returns nonzero if the stream encounters an EOF • bad() Returns nonzero if an error occurs • fail() Returns nonzero if an operation fails • good() Returns nonzero if no state bits are set • clear() Resets the state bits, used at EOF

  9. Application Programming Interface • bool Create(filename) • bool Open(filename) • bool Close(filename) • int Read(data, location) • int Write(data, location) • int Append(data)

  10. General File Access • main() • { • while choice = menu is not quit • { • switch choice: • case open: openFile(aFile) //calls Open • case create: createFile(aFile) //calls Create • case append: appendFile(aFile) //calls Append • case find: findData(aFile) //calls Read • case change: updateData(aFile) //calls Write • case display: displayFile(aFile) //Reads • } • closeFile(aFile) //calls Close • }

  11. Pseudocode for using API bool openFile(file) //attempts to open a file for //reading and writing //Postcondition: file is opened //Return: true if opened, false //otherwise { Open file for input & output if return is false return false else return true } bool createFile(file) //attempts to open a non-existent file //for input and output //Postcondition: file is opened //Return: true if opened, false //otherwise { Create file if return is false return false else return true }

  12. Pseudocode using the API (con’t) bool closeFile(file) //attempts to close file //Postcondition: file closed //Return: true if successful, // false otherwise { Close file if return is false return false else return true } void appendToFile(file) //Gets information from the user and // attempts to // append it to the end of the file //Postcondition: information now in // file { get information from user Append information to file if return is -1 report failure }

  13. Pseudocode using the API (con’t) void updateData(file) //asks the user for which information // to change, asks for new information //attempts to write new information // over old information //Postcondition: information updated { request location of information request updated information Write updated information to requested location if return is not -1 report success else report failure } void findData(file) //asks user what information needed // and attempts to find that // information and display it { request location Read information from requested location if return is not -1 display information else report no such information }

  14. Pseudocode using the API (con’t) void displayFile(file) //displays all information in file for user to // see { Read first piece of information while read is successful display information Read next information }

  15. bool Create(fileName) //attempts to open a file for i/o, if succeeds //Create fails because file exists //Attempt to open for writing only, if fails //Create fails //Close and open for reading and writing open for reading //already exists if success { close,clear & return false } open for writing //success will create if fail { return false } close //so you can reopen to read/write open for reading and writing if success return true else return false bool Open(fileName) //attempt to open for reading and //writing, return success or failure //of open open for reading and writing if success return true else return false Using C++ to Open or Create

  16. Using C++ to Read int Read(data, location) //reads into the parameter data from the offset location //returns number of bytes read if success, -1 otherwise seek to location read information into data if success return number of bytes read else clear return -1

  17. int Write(data, location) //attempts to write info from data to //the offset location. //Returns the offset where written //on success, -1 otherwise seek to location write from data to file if success return offset written else clear return -1 int Append(data) //writes info from data to the end of //file. Returns //offset written if success, -1 otherwise seek to the end of the file offset = current position of cursor write the data to file if success return offset written else clear return -1 Using C++ for Write and Append

  18. Files as Parameters in Functions • files should be passed as reference parameters • Example: void GetText(ifstream &thefile, char *theText); ifstream infile; char Text[81]; GetText(infile, Text);

More Related