180 likes | 343 Views
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.
E N D
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 • Create • Open • Close • Read • Write • Append
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
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();
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
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
Application Programming Interface • bool Create(filename) • bool Open(filename) • bool Close(filename) • int Read(data, location) • int Write(data, location) • int Append(data)
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 • }
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 }
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 }
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 }
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 }
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
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
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
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);