Data File Handling C Data File File is
- Slides: 13
Data File Handling C++
Data File • File is bunch of bytes stored on secondary storage devices. • Program is executed in RAM, all it data get lost after each termination. • When program is attached with a file data can be saved permanently in file. • Basic purpose of file is to store the information which can be used for further reference or processing.
File Types Depending on the way of data storage files are categorized in TEXT and BINARY files. Difference: Text file stores data in form of characters while binary file stores data as its is actually stored in memory (RAM)(binary form). Ex: 2017 in text file will require 04 bytes while binary will require 02 bytes Why binary? – since data is stored in memory through variables, arrays and objects which can be written/accessed/processed directly using binary files.
File Processing Steps i. Determine the file stream ii. Create stream object iii. Open file using stream object iv. Process file v. Close file
File processing steps except PROCESSING are similar for both text and binary files.
File Processing Steps i. Determine the file stream Decides how file is to be processed – read only, write only and read-write. ifstream for read only, ofstream for write only, fstream for both. ii. Create stream object Syntax: streamobj; ex: ifstream fin; iii. Open file using stream object streamobj. open(“filename. ext”, filemodes); iv. Process file v. Close file streamobj. close();
What is Stream? Stream is sequence of bytes/flow of data. It is a kind of channel through which data flow from program to input/output devices & vice versa or file to program & vice versa. It acts as an interface between program to input/output devices. Different streams are used to represent different type of data flows.
Processing a file INPUT DEVICE Input stream cin OUTPUT DEVICE Output stream ofstream PROGRAM [variables/ arrays/ Objects] cout fout FILE ifstream fin RAM Hard Disk
Processing text file Text file stores and access data in the way of characters, so it process file in form of characters, words, lines. Reading (ifstream) Character wise char ch; fin. get(ch) Word wise char w[30]; fin>>w; Line wise char L[80]; fin. getline(L, 80); Writing(ofstream) Character wise fout<<ch; Word wise fout<<w; Line wise fout<<L; Header files ctype. h and string. h functions are useful in processing textual data in form of characters and strings.
Processing binary file Binary file is useful in storing and accessing data in form of objects of class or structure. Reading (ifstream) Classname obj; fin. read((char *)&obj, sizeof(obj)); Writing(ofstream) fin. write((char *)&obj, sizeof(obj));
Detecting end of file while reading 1 while(fin) { : } 2 while(!fin. eof()) { : } 3 while(fin. read(……. )) { : }
Moving file pointer Random access of file pointer can be done with following functions: By default is reading pointer is set at the beginning and writing pointer at end (when you open file in ios: : app mode) Some time we want to move file pointer to specific byte/location (for example to overwrite an object after searching it etc. ) ifstream fin. seekg(byteno, seek_dir) fin. tellg() ofstream fout. seekp(byteno, seek_dir) fout. tellp()
Sample programs for basic operations on Text and Binary files
- File-file yang dibuat oleh user pada jenis file di linux
- Handling files in c
- Cics file handling
- File handling computer science
- Procedure in assembly language
- Stratified sampling gcse
- What is data handling
- What is the information cycle
- Handling data hazards
- Physical image vs logical image
- File sharing management system
- Distributed file system
- Markup tag tells the web browser
- In a file-oriented information system, a transaction file