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.

Slides:



Advertisements
Similar presentations
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
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.
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.
Chapter 8 Data File Basics.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
1 Streams 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.
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
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.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
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
Streams, and File I/O Review. STREAMS Review STREAMS Streams are sequences of bytes. C++ I/0 occurs in streams Input – bytes flow from device to memory.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
1 CSC241: Object Oriented Programming Lecture No 32.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
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.
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.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Lecture 14 Arguments, Classes and Files. Arguments.
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.
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.
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.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
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.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
17 File Processing.
Basic Input and Output Operations
FILE HANDLING IN C++.
Standard Input/Output Streams
Standard Input/Output Streams
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
Chapter 3: Input/Output
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
File I/O in C++ I.
File I/O.
CHAPTER 4 File Processing.
File I/O in C++ II.
File I/O in C++ I.
Presentation transcript:

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 (reads) or transmits (writes) a sequence of bytes. The reading and writing functions handle the streaming of bytes between the devices and memory.

2 cin/cout vs. file I/O The ostream and istream objects cout and cin are generated automatically (they are actually defined in the iostream header file) and associated with the standard output and input devices. In order to read from or write to a file we must manually associate it with an input or output file stream object. We must include the fstream header file for file I/O

3 Sequential files Sequential file = a file containing a collection of data. C++ regards the file as a sequence of bytes. We regard the file as a sequence of records in some specific order and write code to handle the file according to our view of its organization.

4 Sequential files In order to open a file for input or output, we must first create an ifstream or ofstream object and then associate the file with that object: ifstream inFile; // declare an ifstream object ofstream outFile; // declare an ofstream object inFile.open("data.txt", ios::in); // associate file data.txt (in the // current directory) with inFile // and open it for input. outFile.open("results.txt", ios::out);// associate file results.txt (in // the current directory) with // outFile and open it for output.

5 Sequential files More examples: If results.txt already exists, ios::app appends to it, whereas ios::out clears it and then writes to it. fstream inOutFile; // declare an fstream object ofstream outFile; // declare an ofstream object inOutFile.open("data.txt", ios::in | ios::out); // associate file data.txt // with inOutFile and open // it for both input & output outFile.open("results.txt", ios::app);// associate file results.txt with // outFile and open it for output.

6 File I/O You may use the stream insertion and extraction operators to write to or read from a sequential file. For user-defined types, the stream insertion and extraction operators must be overloaded accordingly. ofstream outFile; outFile.open("grades.txt", ios::out); char *name = "John Smith"; double grade = 98.8; outFile << name << " " << grade << endl;

7 File I/O Close the file stream when you don't need the file any longer (if you don't, it will be closed automatically via its destructor when it goes out of scope) ofstream outFile; outFile.open("grades.txt", ios::out); char *name = "John Smith"; double grade = 98.8; outFile << name << " " << grade << endl; outFile.close();

8 operator>> and whitespace operator>> ignores whitespace: What if we want to read the whitespace as well? Use the get() function fstream iofile("test.txt", ios::in|ios::out); iofile << 'a' << ' ' << 'b'; iofile.seekp(0); char x, y; iofile >> x >> y; // here, x is 'a' and y is 'b'. The empty space between them // has been skipped.

9 get() Use #1 : with no arguments ch = inFile.get() ; // reads one character and assigns it to ch Use #2 : with one argument inFile.get(ch); // reads one character and assigns it to ch fstream iofile("test.txt", ios::in|ios::out); iofile << 'a' << ' ' << 'b'; iofile.seekp(0); char x, y; iofile >> x >> y; // here, x is 'a' and y is ' '

10 put(), getline() put() is similar to get() but is used for writing a single character getline(char array, size, delimiter) reads size-1 characters or until the delimiter is found and places the result in the char array (the array will be null terminated). It is used to read whole lines.

11 peek(), seekg(), seekp() inFile.peek() is used to look at the next character in the stream without actually removing it from the stream inFile.seekg(n) moves the get pointer to the nth byte from the beginning of the file outFile.seekp(n) moves the put pointer to the nth byte from the beginning of the file

12 Unformatted I/O To read or write raw bytes, use the read() and write() functions. outFile.write(buffer, n) writes n bytes from buffer to outFile. buffer must be the address of a string. If it is not, typecast it accordingly. inFile.read(buffer, n) reads n bytes from inFile and places them in buffer. buffer must be the address of a string. If it is not, typecast it accordingly.

13 binary mode In many cases we want to open a file in binary mode. Possible reasons: Text files are represented differently in different systems and we want to avoid related peculiarities. Applications such as compression require it. To process a file in binary mode we must open it with ios::binary In that mode, > do NOT work properly. Instead use read() and write().