1 / 15

INTRODUCTION TO COMPUTER

INTRODUCTION TO COMPUTER . Lecture #12 FILES FORMAT. By Shahid Naseem (Lecturer). FILES. A collection of data or information that has a name , called the filename .

nerys
Télécharger la présentation

INTRODUCTION TO COMPUTER

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 COMPUTER Lecture #12 FILES FORMAT By ShahidNaseem (Lecturer)

  2. FILES • A collection of data or information that has a name, called the filename. • Almost all information stored in a computer must be in a file. There are many different types of files: datafiles, text files , program files, directory files, and so on. Different types of files store different types of information. • For example, program files store programs, whereas text files store text. Control Structure (Civil Engineering Department)

  3. FILES Control Structure (Civil Engineering Department)

  4. FILE FORMAT • In a computer, a file format is the layout of a file in terms of how the data within the file is organized. • A program that uses the data in a file must be able to recognize and possibly access data within the file. • File format is often indicated as part of a file's name by a file name extension (suffix). • Word documents (.doc) • Web text pages (.htm or .html) • Web page images (.gif and .jpg) • Adobe Acrobat files (.pdf) • Executable programs (.exe) • Multimedia files (.mp3 and others) Control Structure (Civil Engineering Department)

  5. TERMINOLOGY USED FOR DATA FILES • Characters: Characters consist of alphabets, digits and special characters. These are represented inside the computer as a sequence . It requires 8 bits to store one character. • Fields: A group of characters or bytes represents a piece of data is called field. • Record: A group of related fields is called record. • File: A group of related records is called a file. • The data or records in files can be organized into different ways. In C++, files are processed into two ways. • Formatted input/output • Binary input/output Control Structure (Civil Engineering Department)

  6. STREAMS & FILES • Stream refers to flow of data from a source to a destination. • The process of inputting data from the source is known as reading, extracting, getting or fetching. • The process of outputting data to the destination is known as writing, inserting, putting or storing. • In C++, special classes known as “Stream Classes” are used to handle data streams. • There are three stream classes in C++. • Ifstream • Ofstream • fstream Control Structure (Civil Engineering Department)

  7. STREAMS & FILES • Ifstream: This stream is used to perform input operaions on data files on the disk. The objects of this stream are used to read data from a file on disk into the computer memory. • Ofstream: This stream is used to perform output operations on data files on the disk. The objects of this stream are used to write data from the computer memory into a file on the disk. • Fstream: This stream is used for both input&output operations on files on the disk. • The extraction opeator “>>” is used for input operations is a member of the istream class. • The insertion operator “<<“ is used for output operations is a member of the ostream class. Both are derived from ios class. Control Structure (Civil Engineering Department)

  8. OPENING FILES • Before reading data from or writing data into a file, the file must be opened. • It is opened by creating objects of ifstream,ofstream or fstream stream classes. • A file is opened for output for writing data into it and it is also opened for input for reading data from it. • A file can also be opened for both input and output operations. • To open a data file “filename” in output mode, an object of the ofstream(or fstream) class is created. ofstreamabc(filename, ios:: out); • To open a data file “filename” in input mode ifstreamabc(filename, ios::in) Control Structure (Civil Engineering Department)

  9. FORMATTED INPUT/OUTPUT • Formatted input/output files are also known as Sequence access files. • In sequence access files, the data is read/written in a sequence. To read a specific data item from a file, the file is read from the beginning till the required data item is reached. • This method is very slow. • The data in sequential files cannot be added, updated and deleted. Control Structure (Civil Engineering Department)

  10. Writing data into formatted I/O Files • The insertion operator (<<) is used with “ cout” object to send output to the screen. • The same operator is used with the “object created by ofstream (or fstream) class to send the output to the data file on the disk. • When the porgram end, the object created for the file is also destroyed and the data file attached with the object is automatically closed. Control Structure (Civil Engineering Department)

  11. Reading Data from formatted I/O File • The data written or stored in a formatted input/output file is read back from the file into the memory in the same order in which it is stored. • The data from the file is read by opening the file in input mode. For this purpose, an object of “ifstream(or fstream)” class is created and the same data file in which records are stored is created. • The file is automatically opened when the object is created. • The data from the file is read using the extraction (>>) with the object.s Control Structure (Civil Engineering Department)

  12. WHILE LOOP EXAMPLE Control Structure (Civil Engineering Department)

  13. THE “PUT” FUNCTION • The “PUT” function is used to write a single character at a time into a formatted input/output file. • The syntax of this member function is object. put(character); The “get” Function • The “get” function is used to read a text file one character at a time. • The pointer automatically shifts to the next character after reading a character. object. get(var); Control Structure (Civil Engineering Department)

  14. THE “SEEKP” FUNCTION • The “seekp” stands for “seek put”. • This function is used to seek and put the file pointer position from where the data in the file is to be updated. object . Seekp (pos); Control Structure (Civil Engineering Department)

  15. OUTPUT ON THE PRINTER • The output of the program can also be sent directly to the printer attached with the computer. • Usually the printer is connected to the first parallel port. • The first parallel port is called “PRN” or “LPT1”. • To send the output to the printer, an output object of ofstream(or fstream) is created and the printer port is assigned to it. ofstream pout(“PRN”); Control Structure (Civil Engineering Department)

More Related