Introduction to Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Programming

Description:

Introduction to Programming Lecture 18 File Text Files Executable Programs Memory is volatile Any data that you key in by keyboard while a program is running is also ... – PowerPoint PPT presentation

Number of Views:260
Avg rating:3.0/5.0
Slides: 38
Provided by: Umer4
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming


1
Introduction to Programming
  • Lecture 18

2
File
3
Types of Files
  • Text Files
  • Executable Programs

4
  • Memory is volatile
  • Any data that you key in by keyboard while a
    program is running is also volatile

5
File Handling
  • Text files handling
  • Binary files handling

6
Steps to handle file
  • Open the File
  • Read / Write the File
  • Close the File

7
  • Streams

8
Header File for File Handling
  • fstream.h

9
Header File for File Handling
  • include ltfstream.hgt

10
Input File Stream
  • ifstream

11
Output file stream
  • ofstream

12
Example 1
  • include ltfstream.hgt
  • ifstream myFile
  • myFile.open ( payRoll.txt )

13
Fully Qualified Path Name
  • C\myProg\payRoll.txt

14
Access file data
  • myfile gtgt var1
  • We can also write
  • myfile gtgt var1 gtgt var2

15
Close the File
  • myFile.close ( )

16
Process Open
  • myfile.open ( payRoll.txt )

payRoll.txt
myFile
17
Process Close
  • myfile.close ( payRoll.txt )

payRoll.txt
myFile
X
18
Example 1
  • ifstream myFile
  • myFile.open ( myFile.txt )
  • if ( !myFile ) // Error check
  • cout ltlt Your file could not be opened
  • ------
  • myFile.close ( )

19
Output File Modes
  • Create a new file
  • Overwrite an existing file
  • Append some text
  • Randomly accessing a file

20
Syntax
  • fStream fileVar ( fileName , mode ) //
    Generic syntax
  • ifstream myfile ( myfile.txt , ios in )
  • ofstream myfile ( myfile.txt , ios out )

Opening Mode
Opening Mode
21
List of File Handling Modes
  • ios in open for reading (default for
    ifstream)
  • ios out open for writing (default for
    ofstream)
  • ios app start writing at end of file
    (APPend)
  • ios ate start reading or writing at EOF of
    file (ATEnd)
  • ios trunc truncate file to zero length if
    it exists (TRUNCate)
  • ios nocreate error when opening if file does
    not already exist
  • ios noreplace error when opening for output if
    file already exists
  • ios binary open file in binary (not text)
    mode

22
  • Appendofstream myfile (myfile.txt , ios app
    )
  • Random Access ofstream myfile ( myfile.txt ,
    ios ate )
  • Truncate ofstream myfile ( myfile.txt ,
    iostrunc )

23
myfile.eof ( )
  • while ( !myfile.eof ( ) )
  • myfile gtgt varName

24
get ( )
  • char ch
  • myFile.get ( ch )

25
Example 2
  • while ( !myFile.eof ( ) )
  • myFile.get ( ch )
  • cout ltlt ch

26
put ( )

outputFile.put ( ch )
27
  • ifstream myInputFile ( myfile.txt , ios in
    )
  • ofstream myOnputFile ( myfile.txt , ios out
    )

28
  • int i
  • i 0
  • int i 0

29
Open file
  • ifstream myInputFile ( myfile.txt )

30
Open file
  • ofstream myOnputFile ( myfile.txt )

31
  • strtok ( )

32
getline ( ) function
33
Syntax of getline function
  • myfile.getline (char s, int n, char delim)

Numbers of characters to be read
Character array
delimiter
34
Syntax of getline function
  • Input
  • Hello World
  • myfile.getline ( arrayString , 20 , W )
  • Output
  • Hello

35
File
  • Amir 1000
  • Amara 1002

36
strtok ( string , delimiter )
37
Example
  • char namePtr , salaryPtr , arr 30
  • double fSalary 0.0
  • inFile.getline ( arr , 30 , \n )
  • namePtr strtok ( arr , " " )
  • salaryPtr strtok ( NULL , " " )
  • fSalary atof ( salaryPtr )
Write a Comment
User Comments (0)
About PowerShow.com