Introduction to Programming

Slides:



Advertisements
Similar presentations
Slide: 1 Interra Induction Training Object Oriented Programming in C++ IO Streams Kolkata, July 25, 2005.
Advertisements

C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
“After a day spent staring at a computer monitor, think of a book as a kind of screen saver for your brain” “One good reason why computers can do more.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
File I/O. fstream files File: similar to vector of elements Used for input and output Elements of file can be –character (text)struct –object (non-text.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
C++ Introduction : C++ compilation Standard Input/Output: cin, cout Functions Array, Pointer class File Input/Output.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
1 CSC241: Object Oriented Programming Lecture No 32.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
File Handling in C++.
A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Learners Support Publications Working with Files.
Lecture 14 Arguments, Classes and Files. Arguments.
Files To read a file – We must know its name – We must open it (for reading) – Then we can read – Then we must close it That is typically done implicitly.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
Basic file operations in C++ Dr. Ahmed Telba 1. 2 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
What is wrong in the following function definition
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Basic file operations in C++
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Basic Input and Output Operations
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
FILE HANDLING IN C++.
File I/O Streams, files, strings 1 1.
Interactive I/O Input from keyboard Must prompt user User friendly
آشنایی با ساختارها و کار با فایلها
Today’s Lecture I/O Streams Tools for File I/O
Sequential input and output Operations in file
C++ FileIO pepper.
Chapter 12: Advanced File Operations.
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS150 Introduction to Computer Science 1
File I/O.
Data File Handling in C++
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
File I/O in C++ II.
Lecture 9 Files Handling
Reading from and Writing to Files
Text Files.
Presentation transcript:

Introduction to Programming Lecture 18

File

Types of Files Text Files Executable Programs

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

File Handling Text files handling Binary files handling

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

Streams

Header File for File Handling fstream.h

Header File for File Handling #include <fstream.h>

Input File Stream ifstream

Output file stream ofstream

Example 1 #include <fstream.h> ifstream myFile ; myFile.open ( “payRoll.txt” ) ;

Fully Qualified Path Name C:\myProg\payRoll.txt

myfile >> var1 >> var2 ; Access file data myfile >> var1; We can also write: myfile >> var1 >> var2 ;

Close the File myFile.close ( ) ;

Process : Open myfile.open ( “payRoll.txt” ) ; payRoll.txt myFile

myfile.close ( “payRoll.txt” ) ; Process: Close myfile.close ( “payRoll.txt” ) ; payRoll.txt myFile X

Example 1 ifstream myFile ; myFile.open ( “myFile.txt” ) ; if ( !myFile ) // Error check { cout << “Your file could not be opened”; } ------ myFile.close ( ) ;

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

Syntax fStream fileVar ( “fileName” , mode ) ; // Generic syntax ifstream myfile ( “myfile.txt” , ios :: in ) ; ofstream myfile ( “myfile.txt” , ios :: out ) ; Opening Mode Opening Mode

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

Append ofstream myfile (“myfile.txt” , ios :: app ) ; Random Access ofstream myfile ( “myfile.txt” , ios :: ate ) ; Truncate ofstream myfile ( “myfile.txt” , ios::trunc ) ;

myfile >> varName ; } myfile.eof ( ) while ( !myfile.eof ( ) ) { myfile >> varName ; }

get ( ) char ch ; myFile.get ( ch ) ;

Example 2 while ( !myFile.eof ( ) ) { myFile.get ( ch ) ; cout << ch ; }

put ( ) outputFile.put ( ch ) ;

ifstream myInputFile ( “myfile.txt” , ios :: in ) ; ofstream myOnputFile ( “myfile.txt” , ios :: out ) ;

int i ; i = 0 ; int i = 0 ;

ifstream myInputFile ( “myfile.txt” ) ; Open file ifstream myInputFile ( “myfile.txt” ) ;

ofstream myOnputFile ( “myfile.txt” ) ; Open file ofstream myOnputFile ( “myfile.txt” ) ;

strtok ( )

getline ( ) function

Syntax of getline function myfile.getline (char *s, int n, char delim); Numbers of characters to be read Character array delimiter

Syntax of getline function Input Hello World myfile.getline ( arrayString , 20 , ’W’ ) ; Output Hello

File Amir 1000 Amara 1002

strtok ( string , delimiter )

Example char * namePtr , *salaryPtr , arr [ 30 ] ; double fSalary = 0.0 ; inFile.getline ( arr , 30 , ‘\n’ ) ; namePtr = strtok ( arr , " " ) ; salaryPtr = strtok ( NULL , " " ) ; fSalary = atof ( salaryPtr ) ; :