File Streams and File I/O

Slides:



Advertisements
Similar presentations
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
Advertisements

File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
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.
Arithmetic Operators Operation Operator Example Addition = 9 Subtraction = 1 and = -1 Multiplication * 5 * 4 = 9 Division (integer)
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.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
File I/O ifstreams and ofstreams Sections 11.1 &
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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,
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.
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
1. 2 Review How many bits are in a byte? a) 4 b) 8 c) 16 d) 256.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
C++: Functions, Program Compilation, Libraries Modified from CS101 slides, which are by JPC and JWD © 2002 McGraw-Hill, Inc.
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.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS Computer Science IA: Procedural Programming
CS-103 COMPUTER PROGRAMMING
Functions Manipulating Files
Lab 7: File Input/Output Graham Northup
Basic Input and Output Operations
Copyright © 2003 Pearson Education, Inc.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
Quiz Next Monday.
Data Streams.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
File I/O.
File I/O with Records Lesson xx
More About File Reading
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
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.
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 3 Input output.
File I/O.
File I/O in C++ I.
File I/O.
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
Reading Data From and Writing Data To Text Files
ifstreams and ofstreams
COMS 261 Computer Science I
File I/O in C++ II.
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

File Streams and File I/O

Stream Operators Insertion Operator ‘<<’ As seen with cout << var Extraction Operator ‘>>’ As seen with cin >> var

Stream Declarations #include <fstream> using namespace std; int main() { ifstream fin; // streams data from a file ofstream fout; // streams data to a file

Stream Declarations #include <fstream> using namespace std; int main() { ifstream fin(“input.dat”); //connects this stream // to an existing data // file in the same // directory ofstream fout(“output.dat”); //creates a text file // in the same

Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; ofstream fout; fin.open(“input.dat”); // !may not connect! fout.open(“output.dat”);

Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; ofstream fout; fin.open(“input.dat”); // !may not connect! fout.open(“output.dat”); File may not exist File may be misspelled Perhaps wrong directory etc

Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do fin.clear(); cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin);

Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do fin.clear(); // clears the fail bit – allows retry cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin);

Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do fin.clear(); // clears the fail bit – allows retry cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin);

Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do fin.clear(); // clears the fail bit – allows retry cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin);

Opening a File w/C-strings #include <iostream> #include <fstream> using namespace std; int main() { char file[20]; ifstream fin; do fin.clear(); // clears the fail bit – allows retry cout << “enter name of file to connect to: “; cin.getline(file, 20); fin.open(file); } while(!fin);

Opening a File w/std::strings #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; do fin.clear(); cout << “enter name of file to connect to: “; cin >> file; fin.open(file.c_str()); } while(!fin);

Opening a File w/std::strings #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; do fin.clear(); cout << “enter name of file to connect to: “; cin >> file; fin.open(file.c_str()); } while(!fin);

Opening a File w/std::strings #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; do fin.clear(); cout << “enter name of file to connect to: “; cin >> file; fin.open(file.c_str()); } while(!fin);

Opening a File w/std::strings #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; do fin.clear(); cout << “enter name of file to connect to: “; cin >> file; fin.open(file.c_str()); } while(!fin);

Reading a File input.dat 3 -1 34 56 3 14 12 6 124 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat 3 -1 34 56 3 14 12 6 124

Reading a File input.dat 3 -1 34 56 3 14 12 6 124 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat 3 -1 34 56 3 14 12 6 124

Reading a File input.dat 3 -1 34 56 3 14 12 6 124 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat 3 -1 34 56 3 14 12 6 124

Reading a File input.dat 3 -1 34 56 3 14 12 6 124 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat 3 -1 34 56 3 14 12 6 124

Reading a File input.dat 3 -1 34 56 3 14 12 6 124 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file fin >> num; etc; input.dat 3 -1 34 56 3 14 12 6 124

Closing a File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string file; ifstream fin; int num; ... // code for opening the file ... // code for reading from the file fin.close();

More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87

More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87

More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87

More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87

More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87

More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87

More Reads input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file string last, first; int age; fin >> last; fin >> first; fin >> age; etc. input.dat Price Clayton 12 Hurson Ali 41 Buechler Matt 87

More Reads #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file int class_num, course; char section; string title; input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures

More Reads #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file int class_num, course; char section; string title; input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures

More Reads #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file int class_num, course; char section; string title; input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures

More Reads #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file int class_num, course; char section; string title; input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures

More Reads #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file int class_num, course; char section; string title; fin >> class_num; fin >> course; fin >> section; getline(fin, title, ‘\n’); input.dat 51324 53 A Intro to Programming 51325 53 B Intro to Programming 51326 53 C Intro to Programming 51334 128 A Discrete Mathematics 51335 128 B Discrete Mathematics 51344 153 A Data Structures 51345 153 B Data Structures

More Reads input.dat Price, Clayton Hurson, Ali Buechler, Matt #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file string last, first; getline(fin, last, ‘,’); getline(fin, first, ‘\n’); etc. input.dat Price, Clayton Hurson, Ali Buechler, Matt Van Horn, Keith Walker, Betty Sue

End of Session