Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI

Slides:



Advertisements
Similar presentations
By Sumit Kumar Gupta PGT(CS) KV NO.1, 1st Shift, BBSR.
Advertisements

CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace.
File Handling Accessing a data file for input: 1) Include the file stream header file: #include using namespace std; 2) Associate an input file name with.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
DATA FILE HANDLING. Contents to be covered in class  Concept of files & streams  Stream class hierarchy  File handling functions  Text V/S Binary.
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.
Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
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.
C++ FILE I/O.
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,
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.
Student Book Input / Output in C++
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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++.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
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.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
Learners Support Publications Working with Files.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
Lecture 14 Arguments, Classes and Files. Arguments.
FILE HANDLING(WORKING WITH FILES) FILE- A file is a collection of related data stored in a particular area on the disk. STREAMS- interface between the.
Binary Files. Text Files vs. Binary Files Text files: A way to store data in a secondary storage device using Text representation (e.g., ASCII characters)
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.
Data File Handling in C++ Data File Handling in C++ Paritosh Srivastava PGT (Comp. Science) Kendriya VidyalayaJoshimath.
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.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
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
CPSC 231 D.H. C++ File Processing
FILE HANDLING IN C++.
Basic File I/O and Stream Objects
آشنایی با ساختارها و کار با فایلها
files Dr. Bhargavi Goswami Department of Computer Science
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Sindhu S PGT Comp.Sc. K V Kollam
Data File Handling in C++
CHAPTER 4 File Processing.
C++ Programming: chapter 6 – iostream
Data File Handling RITIKA SHARMA.
Reading from and Writing to Files
File Handling Accessing a data file for input: 1) Include the file stream header file: #include using namespace std; 2) Associate an input file.
File I/O in C++ II.
Reading from and Writing to Files
Input/Output Streams, Part 2
Presentation transcript:

Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI Input/Output Streams File IO

Dale Roberts 2 File Streams Provides high level support for file operations in C++ Consists of three components: ifstream - allows only input ifstream - allows only input ofstream - allows only output ofstream - allows only output fstream - allows both input and output fstream - allows both input and output

Dale Roberts 3 Files -- fstream class Inherits from fstreambase and iostream classes fstreambase inherits from ios class iostream – inherits from istream and ostream classes ifstream – inherits from fstreambase and istream classes ofstream – inherits from fstreambase and ostream classes

Dale Roberts 4 Class Hierarchies ios istreamostream iostream fstreambase ifstreamofstream fstream

Dale Roberts 5 File Stream Functions Contains the following functions: open() - opens the stream open() - opens the stream close() - closes the stream close() - closes the stream attach() - attaches the stream to file descriptor attach() - attaches the stream to file descriptor setbuf() - set the stream buffer setbuf() - set the stream buffer rdbuf() - returns the pointer to stream buffer rdbuf() - returns the pointer to stream buffer str() - returns a pointer to the buffer array str() - returns a pointer to the buffer array

Dale Roberts 6 File Streams - Syntax Syntaxfstream(); fstream(const char *sFileName, int nMode, int nProt = 0664); fstream(filedesc fdif);

Dale Roberts 7 File Processing Modes nMode can be any of the following: ios::in - input processing ios::out - output processing ios::trunc - discard the file contents ios::nocreate - the file must exist in order to write ios::noreplace - cannot rewrite to an existing file ios::binay - open the file in binary mode ios::app - append the new data to the contents of the file ios::ate - open the file for output and move to the end ios::in|ios::out - both input and output

Dale Roberts 8 File Protection Modes nProt can be one of the following: filbuf::shcompat - compatibility share mode filbuf::shcompat - compatibility share mode filebuf::sh_none - no sharing filebuf::sh_none - no sharing filebuf::sh_read - read sharing filebuf::sh_read - read sharing filebuf::sh_write - write sharing filebuf::sh_write - write sharing filebuf::sh_read|filebuf::sh_write - both read and write sharing filebuf::sh_read|filebuf::sh_write - both read and write sharing

Dale Roberts 9 File Streams - Example fstream my_file; fstream myfile(“cs265.txt”, ios::in) fstream myfile(“cs265.txt”, ios::out) const int nMAX_SIZE = 256; char buf[nMAX_SIZE]; filedesc fdesc = my_file.fd(); fstream fs(fdesc);

Dale Roberts 10 File Streams - open() Opens a file as a stream Syntax void open(const char *sfile, int nMode, int nProt = 0664); Usage fstream my_file; my_file.open(“cs265.txt”, ios::out);

Dale Roberts 11 File Streams - close() Close the opened file. The stream’s error flag is cleared unless the close fails. Syntax void close() Usagemy_file.close();

Dale Roberts 12 File I/O - Example #include #include #include #include main() { // fIn and fOut are objects of class fstream fstream fIn, fOut; int nCnt = 0; //for byte count; char cCh; //Open the file copy.in for reading fIn.open("copy.in", ios::in); //Open the file copy.in for reading fIn.open("copy.in", ios::in); //Open the file copy.out for writing fOut.open("copy.out", ios::out); //Open the file copy.out for writing fOut.open("copy.out", ios::out);

Dale Roberts 13 File I/O – Example contd… //Until the end of file is reached, read from //the file and write it to the out file and echo on the screen. while(fIn.get(cCh)) { fOut.put(cCh); cout.put(cCh); //echo on terminal ++nCnt; } //Until the end of file is reached, read from //the file and write it to the out file and echo on the screen. while(fIn.get(cCh)) { fOut.put(cCh); cout.put(cCh); //echo on terminal ++nCnt; } //Write the final byte count cout << "[ " << nCnt << " ]" << endl; fOut << nCnt; //Write the final byte count cout << "[ " << nCnt << " ]" << endl; fOut << nCnt; //Close the files fIn.close(); fOut.close(); //Close the files fIn.close(); fOut.close(); return 0;} return 0;}

Dale Roberts 14 File I/O - Files copy.in abcd efg hi j copy.out abcd efg hi j 14