CSCI 333 Data Structures I/O with C++ 20 October, 2003.

Slides:



Advertisements
Similar presentations
IOStreams CNS 3370 Copyright 2003, Fresh Sources, Inc.
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 in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
File I/O Finished off Colin Cherry standing in for Dr. Lin.
CPSC 231 D.H. C++ File Processing 1 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random access files.
計算機概論實習 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.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Streams, 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.
C++ Programming Certificate University of Washington Cliff Green
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
File I/O ifstreams and ofstreams Sections 11.1 &
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
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.
Fundamental File Processing Operations C++
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.
C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
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,
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
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
CS  Inserters and Extractors  Stream State  Files Streams  String Streams  Formatting  Manipulators  Internationalization.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
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 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.
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.
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)
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.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
2008YeungNam Univ. SE Lab. 1 C++ views each file as a sequence of bytes terminated by EOF-marker. Header files Files are opened by creating objects of.
CS212: Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
File Processing.
Programming with ANSI C ++
What is wrong in the following function definition
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.
CISC/CMPE320 - Prof. McLeod
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
Topics Input and Output Streams More Detailed Error Testing
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CISC/CMPE320 - Prof. McLeod
C++ Programming: chapter 6 – iostream
C++ Programming Lecture 8 File Processing
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
File I/O in C++ II.
Input/Output Streams, Part 2
Presentation transcript:

CSCI 333 Data Structures I/O with C++ 20 October, 2003

C++ Stream I/O hierarchy ios istream ostream iostream ifstream multiple inheritance virtual base class virtual base class fstream ofstream

ios class Formatting –Width of field, base of numbers State –Open, closed, end-of-file, failed Constants –Open for read/write/append Buffering –Using streambuf class

ANSI C++ ios To support internationalization –ios is a template of basic_ios –Specialized for character set But western programmers don’t worry –Except when looking at the documentation

ios flags for opening files ios::in Open for input ios::out Open for output ios::app Append to file ios::trunc Truncate file ios::nocreate File if file doesn’t exist ios::noreplace Fail if file exists

ios state methods F.good() No errors F.fail() Failure, stream usable F.bad() Failure, stream not usable F.eof() End-of-file !F!F Failed or bad

ios formatting methods F.setw(int) Sets width, returns the stream F.width(int) Sets width returns current width F.fill(char) Sets fill character returns current fill

ios flags for moving in files ios::beg Move relative to beginning of file ios::cur Move relative to current position ios::end Move relative to end of file

istream class Stream input class Read from files –to all sorts of variables Seek to arbitrary file position Templated in ANSI C++ –from basic_istream

Some istream methods F >> x Extract from stream F.read(buff, n) Extract into buffer F.seekg(newPos) Move get pointer F.seekg(nP,skpDr) Move get pointer, relative to seek direction F.tellg() Returns get pointer

Some ostream methods F << x Insert into stream F.write(buff, n) Insert from buffer F.seekp(newPos) Move put pointer F.seekp(nP,skpDr) Move put pointer, relative to seek direction F.tellp() Returns put pointer

The iostream methods Multiple inheritence –istream –ostream Single ios subclass –Since ios is a virtual subclass

The standard stream s cin Standard input cout Standard output cerr Standard error

The  fstream methods Most inherited from *stream methods –  fstream F ; –  fstream F(filename, flags) ; –F.open(filename, flags) ; –F.close() ;

// Opening and closing with ifstream and ofstream #include using namespace std ; int main(int argc, char *argv[]) { ifstream RStream ; ofstream WStream ; RStream.open(argv[1]) ; if (RStream.bad()) cerr << "Unable to read " << argv[1] << endl ; RStream.close() ; WStream.open(argv[1]) ; if (WStream.bad()) cerr << "Unable to write " << argv[1] << endl ; WStream.close() ; }

// Opening and closing with fstream #include using namespace std ; int main(int argc, char *argv[]) { fstream RWStream ; RWStream.open(argv[1], ios::in) ; if (RWStream.bad()) cerr << "Unable to read " << argv[1] << endl ; RWStream.close() ; RWStream.open(argv[1], ios::out) ; if (RWStream.bad()) cerr << "Unable to write " << argv[1] << endl ; RWStream.close() ; }

// Silly file updates #include using namespace std ; int main(int argc, char *argv[]) { fstream RWStream ; RWStream.open(argv[1], ios::in | ios::out | ios::binary) ; if (RWStream.bad()) cerr << "Unable to read or write" << argv[1] << endl ; else { char buff[7] ; // Read from characters 7 to 13 RWStream.seekg(7, ios::beg) ; RWStream.read(buff, 7) ; // Write to characters 14 to 20 RWStream.seekp(14, ios::beg) ; RWStream.write(buff, 7) ; RWStream.close() ; }

// Reverse a file #include using namespace std ; int main(int argc, char *argv[]) { fstream RWStream ; RWStream.open(argv[1], ios::in | ios::out | ios::binary) ; if (RWStream.bad()) cerr << "Unable to read or write" << argv[1] << endl ; else { char buffFront[1] ; // extremely inefficient char buffRear[1] ; RWStream.seekp(0, ios::end) ; int fileSize = RWStream.tellp() ; for (int i=0; i<fileSize/2; ++i) { RWStream.seekg(i, ios::beg) ; RWStream.read(buffFront, 1) ; RWStream.seekg(fileSize-i-1, ios::beg) ; RWStream.read(buffRear, 1) ; RWStream.seekp(i, ios::beg) ; RWStream.write(buffRear, 1) ; RWStream.seekp(fileSize-i-1, ios::beg) ; RWStream.write(buffFront, 1) ; } RWStream.close() ; }

// writing numbers in ASCII and binary #include using namespace std ; int main(int argc, char *argv[]) { ofstream WStream ; WStream.open(argv[1], ios::binary) ; if (WStream.bad()) cerr << "Unable to write" << argv[1] << endl ; else { for (int i=333; i<343; ++i) WStream << i ; for (int i=333; i<343; ++i) WStream.write((char *)&i, sizeof(i)) ; } WStream.close() ; }

// formatted I/O with ios methods #include using namespace std ; int main(int argc, char *argv[]) { cout.fill('0') ; for (int i=0; i<100; ++i) { cout.width(2) ; cout << i ; cout.width(4) ; cout << i*i << endl ; }

// formatting with io manipulators #include using namespace std ; int main(int argc, char *argv[]) { cout.fill('0') ; for (int i=0; i<100; ++i) cout << setw(2) << i << " " << setw(4) << i*i << endl ; }

// Reading integers or characters #include using namespace std ; int main(int argc, char *argv[]) { int i ; cin >> i ; if (cin.fail()) { cin.clear() ; char c ; cin >> c ; if (cin.fail()) cout << "Failure on I/O" ; else cout << "Read character '" << c << "'" << endl ; } else cout << "Read integer " << i << endl ; }

// Summing integers #include using namespace std ; int main(int argc, char *argv[]) { int i ; int sum = 0 ; while(cin >> i && cin.good()) sum += i ; cout << "Total = " << sum << endl ; }

#include using namespace std ; #define INBUFFSIZE 4096 int main(int argc, char *argv[]) { int sum = 0 ; char inLine[INBUFFSIZE+1] ; while (cin.getline(inLine, INBUFFSIZE) && cin.gcount()) { int lineSum = 0 ; int i ; istringstream lineStream(inLine) ; while (lineStream>>i && lineStream.good()) lineSum += i ; if (lineStream.eof()) sum += lineSum ; else cerr << "Ignoring bad input line: " << inLine << endl ; } cout << "Sum = " << sum << endl; }