I/O in C++ October 7, 2008. 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.

Slides:



Advertisements
Similar presentations
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
Advertisements

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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
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.
Chapter 15.
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.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Fundamental Programming Command Line Parameters and File I/O.
C++ Lecture 8 Monday, 25 August I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
Chapter 9 I/O Streams and Data Files
FILE HANDLING IN C++.
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 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.
“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.
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.
C++ FILE I/O.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Member Functions for Reading and Writing Files 12.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.
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++.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
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.
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.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
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 ++
17 File Processing.
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
Today’s Lecture I/O Streams Tools for File I/O
Chapter 13: Advanced File and I/O Operations
when need to keep permanently
C++ FileIO pepper.
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
Sindhu S PGT Comp.Sc. K V Kollam
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Data File Handling in C++
C++ Programming: chapter 6 – iostream
C++ Programming Lecture 8 File Processing
C++ for Engineers and Scientists Second Edition
Presentation transcript:

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 streams: from the program note I use plural  one program can have multiple I/O streams associated  and vice-versa

Input/Output Console based, no GUI standard streams:  cin: standard input  cout: standard output  cerr: standard error

Extraction/Insertion cout << “Hello world!”; cout << “The value of i is “ << i << endl; //endl puts a new line cout << “Please enter your name: “; string name; cin >> name;

What are cin and cout? Stream classes Classes have methods, as we know  so does cin and cout  some common to all I/O stream classes in C++  File I/O, binary/text mode I/O, console I/O

One example cin inputs ints, chars, null-terminated strings, string objects  but terminates when encounters space (ascii character 32)‏  workaround?  use the “get” method

Snippet char tData[100]; // This is a method in C++ istream classes for // inputting text //including spaces cin.get( tData, 99 ); // or cin.get(tData,99,'\n'); Inputting “i am oh so cool” cin.get gets the entire line just cin will get “I”  space termination

Or, Use the getline function getline( cin, name );

File I/O Reading from or writing to files on disk ifstream and ofstream classes  dedicated for input and output respectively or, use fstream

Example Files Program(filesdemo) ofstream myofile; myofile.open( “sample.txt” ); myofile << “This is a sample line I'm writing\n”; myofile.close();... ifstream myifile; myifile.open( “sample.txt” ); string oneLine; getline( myifile, oneLine ); cout << oneLine; myifile.close();

Read/Write to files (files1/2)‏ Similar to how we use cin and cout  remember, these are I/O streams too myfile is a file stream object, then:  to write an int: int i = 10; myfile << i;  to read an int: int i; myfile >> i;

Binary files As opposed to text files, they are unformatted as ascii.  text files stores everything as ascii text strings  even numbers  binary files do not Example: consider outout of the program in the previous slide

Difference? Example program  Accepts student ID (I input 1010)  Accepts name (I input Junaed)  Accepts CGPA (I input 4.5) Save into two files, as text and binary

Storage TEXT FILE BINARY FILE

Binary files Files by default are text Different methods to write and read  requires casting (we'll see casting soon)‏  different data format If time permits, we'll revisit

Failures? If open fails?  Check before use  if( !myifile ) { cerr << “Cannot open file!”; exit(1); } End of file?  while( myifile.fail() ) { //do your operations here }

Random vs Sequential Random access files  nonsequential,  as a result faster access times,  content must be suitable for random access for example. not on network streams! or console input

File “heads” Access positions one each for read and write hence two methods:  seekg (as in “get”) for reading  seekp (as in “put”) for writing  ifstreams have seekg  ofstreams have seekp

seeking seekg( position, mode) //(same for seekp)‏ position is a long integer signed offset mode can be  ios::beg: from the beginning  ios::end from the end  ios::cur from current position

telling tellg and tellp  returns as long integer, the position of the get and put positions, respectively

example seeks file.seekg( 20L, ios::beg ); file.seekp( -100L, ios::cur ); long pPosition = file.tellp(); long gPosition = file.tellg();