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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
File input and output in C++ Dr. Ahmed Telba. CSE202: Lecture 9The Ohio State University2 Reading & Writing to Files.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 8 Data File Basics.
File I/O ifstreams and ofstreams Sections 11.1 &
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.
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.
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.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
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.
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
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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.
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.
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.
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++. 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.
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++
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
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Basic file operations in C++
CS-103 COMPUTER PROGRAMMING
Lecture 9 Files, Pointers
Basic Input and Output Operations
Copyright © 2003 Pearson Education, Inc.
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Lecture 5A File processing Richard Gesick.
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
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
CHAPTER 4 File Processing.
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
ifstreams and ofstreams
File I/O in C++ II.
Lecture 9 Files Handling
File I/O in C++ I.
Reading from and Writing to Files
Input/Output Streams, Part 2
Text Files.
Programming Fundamental
Presentation transcript:

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.

File Names All files are assigned a name that is used for identification purposes by the operating system and the user.

Focus on Software Engineering: The Process of Using a File Using a file in a program is a simple three-step process The file must be opened. If the file does not yet exits, opening it means creating it. Information is then saved (write)to the file, read from the file, or both. When the program is finished using the file, the file must be closed.

C++ Files and Streams Classes for file stream operation ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files. These classes are derived directly or indirectly from the classes istream and ostream.

C++ Files and Streams C++ views each files as a sequence of bytes. Each file ends with an end-of-file marker. When a file is opened, an object is created and a stream is associated with the object. To perform file processing in C++, the header files <iostream> and <fstream> must be included. <fstream> includes <ifstream> and <ofstream>

open a file open (“filename.extension”, mode); The first operation generally performed on an object of one of these classes is to associate it to a real file. This procedure is known as to open a file. open (“filename.extension”, mode); Ofstream outClientFile(“clients.dat”) OR Ofstream outClientFile(“clients.dat”, ios:out) Combining object construction and stream opening in a single statement. Both forms to open a file are valid and equivalent. Ofstream outClientFile; outClientFile.open(“clients.dat”, ios:out)

File Open Modes ios::in Open for input operations. ios::out Open for output operations. ios::binary Open in binary mode. ios::ate Set the initial position at the end of the file. If this flag is not set, the initial position is the beginning of the file. ios::app All output operations are performed at the end of the file, appending the content to the current content of the file. ios::trunc If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.

Testing for Open Errors is_open() To check if a file stream was successful opening a file you can do it by calling to member is_open. This member function returns a bool value i.e. true or false. True in the case that indeed the stream object is associated with an open file, or false otherwise: if ( myfile.is_open() ) { /* ok, proceed with output */ }

Testing for Open Errors dataFile.open(“cust.dat”, ios::in); if (!dataFile) { cout << “Error opening file.\n”; }

Another way to Test for Open Errors dataFile.open(“cust.dat”, ios::in); if ( dataFile.fail() ) { cout << “Error opening file.\n”; }

How to close a file in C++? The file is closed implicitly when a destructor for the corresponding object is called OR by using member function close: myfile.close(); Once this member function is called, the stream object can be re-used to open another file, and the file is available again to be opened by other processes.

Text file streams are those where the ios::binary flag is not included in their opening mode. These files are designed to store text and thus all values that are input or output from/to them can suffer some formatting transformations, which do not necessarily correspond to their literal binary value.

WRITING ON A TEXT FILE #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << “MY NAME IS SAHER JAWAID\n"; myfile << “I AM LECTURER AT .\n"; myfile << “SIR SYED UNIVERSITY.\n"; myfile.close(); } else cout << "Unable to open file"; return 0;

READING A TEXT FILE cout << line << '\n'; #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) cout << line << '\n'; } myfile.close(); else cout << "Unable to open file"; return 0;

This last example reads a text file and prints out its content on the screen. We have created a while loop that reads the file line by line, using getline. The value returned by getline is a reference to the stream object itself, which when evaluated as a Boolean expression True if the stream is ready for more operations False if either the end of the file has been reached if some other error occurred.

//place the line from myfile into the //line variable: while ( ! myfile.eof() ) { //place the line from myfile into the //line variable: getline (myfile,line); //display the line we gathered: cout << line << endl; }

eof() which stands for "end of file". The eof() function is a boolean function check whether or not the file has reached the end. It returns true when the file is at the end and false otherwise.

for (int count = 0; count < 4; count++) { dataFile >> name; cout << name << endl; }

for (int count = 0; count < 4; count++) { dataFile >> name; cout << name << endl; }

Checking state flags bad() fail() Returns true if a reading or writing operation fails. For example, in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left. fail() Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number.

eof() Returns true if a file open for reading has reached the end. good() It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true. Note that good and bad are not exact opposites (good checks more state flags at once). clear() can be used to reset the state flags.