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,

Slides:



Advertisements
Similar presentations
Passing Streams to Functions. Passing Streams to Functions One Rule: always pass a stream as a reference.
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 Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
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.
STARTING OUT WITH STARTING OUT WITH Class 15 Honors.
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.
計算機概論實習 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.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
File I/O Supplemental Material. Background In C++, files can be manipulated in the same manner we manipulate streams such as: cout and cin. Therefore,
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.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
File I/O ifstreams and ofstreams Sections 11.1 &
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
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.
1 firstlastnextprevioushome richard jones COMP103A Introduction to Computer Science 1 Richard Jones G2.04
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.
“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.
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.
File I/O Version 1.0. Topics I/O Streams File I/O Formatting Text Files Handling Stream Errors File Pointers.
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
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.
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.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
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)
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.
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
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.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
File Processing.
What is wrong in the following function definition
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS-103 COMPUTER PROGRAMMING
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.
Programming with Data Files
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O in C++ I.
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
File I/O in C++ II.
Lecture 9 Files Handling
File I/O in C++ I.
Reading from and Writing to Files
Text Files.
Presentation transcript:

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, openmode mode); Void ifstream ::open ( const char *filename, openmode mode= ios:: in); Void ofstream :: open ( const char *filename, openmode mode= ▫ios:: out | iso :: trunc);

File Open Mode NameDescription ios::inOpen file to read ios::outOpen file to write ios::appAll the data you write, is put at the end of the file. It calls ios::out ios::ateAll the data you write, is put at the end of the file. It does not call ios::out ios::truncDeletes all previous content in the file. (empties the file) ios::nocreateIf the file does not exists, opening it with the open() function gets impossible. ios::noreplaceIf the file exists, trying to open it with the open() function, returns an error. ios::binaryOpens the file in binary mode.

File Open Mode #include using namespace std; void main() { ofstream outFile("file1.txt", ios::out); outFile << "That's new!\n"; outFile.close(); } If you want to set more than one open mode, just use the OR operator- |. This way: ios::ate | ios::binary

File format In c++ files we (read from/ write to) them as a stream of characters What if I want to write or read numbers ?

Example writing to file #include using namespace std; void main() { ofstream outFile; // open an exist file fout.txt outFile.open("number.txt",ios::app); if (!outFile.is_open()) { cout << " problem with opening the file ";} else {outFile <<200 <<endl ; cout << "done writing" <<endl;} outFile.close(); }

Example Reading from file #include using namespace std; void main() {//Declare and open a text file ifstream INFile("number.txt"); string line; int total=0; while(! INFile.eof()) {//fetch line from number.txt and put it in a string getline(INFile, line); //converting line string to int stringstream(line) >> total; cout << line <<endl; cout <<total +1<<endl;} INFile.close(); // close the file }