Top Banner
INTRODUCTION TO COMPUTER Lecture #12 FILES FORMAT By Shahid Naseem (Lecturer)
15

INTRODUCTION TO COMPUTER

Feb 24, 2016

Download

Documents

NERYS

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 . - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: INTRODUCTION TO COMPUTER

INTRODUCTION TO COMPUTER

Lecture #12

FILES FORMAT

ByShahid Naseem

(Lecturer)

Page 2: INTRODUCTION TO COMPUTER

2

FILES

Control Structure (Civil Engineering Department)

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: data files, 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.

Page 3: INTRODUCTION TO COMPUTER

3

FILES

Control Structure (Civil Engineering Department)

Page 4: INTRODUCTION TO COMPUTER

4

FILE FORMAT

Control Structure (Civil Engineering Department)

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)

Page 5: INTRODUCTION TO COMPUTER

5

TERMINOLOGY USED FOR DATA FILES

Control Structure (Civil Engineering Department)

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

Page 6: INTRODUCTION TO COMPUTER

6

STREAMS & FILES

Control Structure (Civil Engineering Department)

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++.IfstreamOfstreamfstream

Page 7: INTRODUCTION TO COMPUTER

7

STREAMS & FILES

Control Structure (Civil Engineering Department)

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.

Page 8: INTRODUCTION TO COMPUTER

8

OPENING FILES

Control Structure (Civil Engineering Department)

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.

ofstream abc(filename, ios:: out);To open a data file “filename” in input mode

ifstream abc(filename, ios::in)

Page 9: INTRODUCTION TO COMPUTER

9

FORMATTED INPUT/OUTPUT

Control Structure (Civil Engineering Department)

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.

Page 10: INTRODUCTION TO COMPUTER

10

Writing data into formatted I/O Files

Control Structure (Civil Engineering Department)

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.

Page 11: INTRODUCTION TO COMPUTER

11

Reading Data from formatted I/O File

Control Structure (Civil Engineering Department)

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

Page 12: INTRODUCTION TO COMPUTER

12

WHILE LOOP EXAMPLE

Control Structure (Civil Engineering Department)

Page 13: INTRODUCTION TO COMPUTER

13

THE “PUT” FUNCTION

Control Structure (Civil Engineering Department)

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 isobject. put(character);

The “get” FunctionThe “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);

Page 14: INTRODUCTION TO COMPUTER

14

THE “SEEKP” FUNCTION

Control Structure (Civil Engineering Department)

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);

Page 15: INTRODUCTION TO COMPUTER

15

OUTPUT ON THE PRINTER

Control Structure (Civil Engineering Department)

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”);