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,

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
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.
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.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
File streams Chapter , ,
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.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
Program Input and the Software Design Process ROBERT REAVES.
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.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
Chapter 8 Data File Basics.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
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.
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.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
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++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
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.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
1 Chapter 4 Program Input and the Software Design Process.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
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.
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
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 Computing an Optimal Code for a Document.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
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.
Basic Input and Output Operations
Standard Input/Output Streams
Standard Input/Output Streams
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
Chapter 3: Input/Output
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
File I/O.
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.
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

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, the same conventions apply. File << “Data”; File << “Data”; Inserts “Data” into file at current location Inserts “Data” into file at current location File >> data; File >> data; Extracts from File into data – hopefully the type matches. Extracts from File into data – hopefully the type matches. These files can be passed to our overloaded > operators in order to function on files instead of cout/cin.

Reading Files Unlike cout/cin we need to explicitly declare how we use our file I/O. ifstream infile; ifstream infile; infile.open(“someinputfile.txt”,ios::in); infile.open(“someinputfile.txt”,ios::in); int x; int x; infile >> x; infile >> x; infile.close(); infile.close(); The above code opens a file and reads from the beginning of the file (until whitespace, treating as an int) into x. Unless we used ios::binary, you can think of the file similarly to someone at the keyboard typing in the equivalent input as what is in the file.

Reading Files - Methods infile.get(char [] storage, int amount, char condition) Gets “amount” characters and puts them into “storage”. Stops if “condition” is seen. Gets “amount” characters and puts them into “storage”. Stops if “condition” is seen. getline(istream & in, string target, char condition) Gets characters from “in” and puts them in “target”. Stops when “condition” or end of file occurs. Gets characters from “in” and puts them in “target”. Stops when “condition” or end of file occurs. infile.ignore(int amount, char condition) Skip over “amount” characters, including (but stopping after) “condition”. get and getline stop at “condition”, so ignore is helpful. Skip over “amount” characters, including (but stopping after) “condition”. get and getline stop at “condition”, so ignore is helpful.

Reading Files - Methods infile.seekg(int offset,ios::beg) Go to beginning of infile + offset Go to beginning of infile + offset infile.seekg(int offset,ios::cur) Move from current location by offset Move from current location by offset infile.seekg(int offset,ios::end) Go to end of infile + offset Go to end of infile + offset int infile.tellg() Returns offest from beginning of infile Returns offest from beginning of infileinfile.close() bool infile.good() Return true if the last read was ok Return true if the last read was ok bool infile.eof() Return true if we are at the end of the file Return true if we are at the end of the file

Writing Files ofstream outfile; ofstream outfile; outfile.open(“someoutputfile.txt”,ios::out); outfile.open(“someoutputfile.txt”,ios::out); string x(“stuff I am writing to a file”); string x(“stuff I am writing to a file”); outfile << x; outfile << x; outfile.close(); outfile.close(); The above code opens a file writes the string x into the file. If you replaced all of our cout statements with an ofstream variable all your output would go to the file instead of the screen. This is why our overloaded << takes in an ostream. It won’t always be cout, it could be access to a file for writing.

Writing Files - Methods outfile.write(char [] towrite, int amount) Writes “amount” characters from “towrite” into outfile. Remember, you can use a “string” by using “.c_str()” Writes “amount” characters from “towrite” into outfile. Remember, you can use a “string” by using “.c_str()” Position Methods: Same as reading outfile.seekp(int offset, ios::beg) outfile.seekp(int offset, ios::beg) outfile.seekp(int offset, ios::cur) outfile.seekp(int offset, ios::cur) outfile.seekp(int offset, ios::end) outfile.seekp(int offset, ios::end) outfile.tellp() outfile.tellp()

Writing Files - Methods outfile.close() outfile.open(char [] outputfilename,ios::out) Opens “outputfilename”, writes occur at the beginning of the file and are destructive. Opens “outputfilename”, writes occur at the beginning of the file and are destructive. outfile.open(char [] outputfilename,ios::app) All writes are appended to the end of the file. No pre- existing data is lost. All writes are appended to the end of the file. No pre- existing data is lost. outfile.open(char [] outputfilename,ios::binary) Data is written as binary rather than text (for generating non-text files). Data is written as binary rather than text (for generating non-text files).

Output Example #include<iostream>#include<fstream> using namespace std; int main() { ofstream outfile; outfile.open(“outputfile.txt”,ios::out); cout << “Count to what?” << endl; int num; cin >> num; for(int i=1;i<=num;i++) { outfile << i << endl; }outfile.close(); return 0; } User Input: 7 Contents of outputfile.txt:

Input Example #include<iostream>#include<fstream> using namespace std; int main() { ifstream infile; infile.open(“inputfile.txt”,ios::in); cout << “Add what to each value in the file?” << endl; int num; cin >> num; while(!infile.eof()){ int temp; infile >> temp; cout << temp + num << endl; }infile.close(); return 0; } Contents of inputfile.txt: User Input: 12 Output: