Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011

Slides:



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

CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
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.
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 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.
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.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Input/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.
Chapter 8 Data File Basics.
File I/O ifstreams and ofstreams Sections 11.1 &
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.
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.
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,
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
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.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
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 file operations in C++ Dr. Ahmed Telba 1. 2 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
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++
Introduction to Programming
Lecture 9 Files, Pointers
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.
C ++ MULTIPLE CHOICE QUESTION
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Data Streams.
Standard Input/Output Streams
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
آشنایی با ساختارها و کار با فایلها
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Today’s Lecture I/O Streams Tools for File I/O
C++ FileIO pepper.
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.
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]
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
Reading Data From and Writing Data To Text Files
ifstreams and ofstreams
Strings Skill Area 313 Part C
File I/O in C++ II.
Lecture 9 Files Handling
Reading from and Writing to Files
Text Files.
Presentation transcript:

Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell,

Reading a variable from keyboard input.

input from keyboard // i/o example #include using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }

input from keyboard // i/o example #include using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; } cin takes input from the keyboard and moves it into a variable.

input from keyboard // i/o example #include using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; } cin works similarly to cout, except in reverse. Data is coming from the keyboard and moved into variables.

input from keyboard // i/o example #include using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; } You have to press enter on the keyboard for cin to register the input and move it into the variable (or variables specified).

Reading multiple variables from keyboard input.

input from keyboard // i/o example #include using namespace std; int main () { int i, j; cout << "Please enter two integer values: "; cin >> i >> j; cout << "The values you entered are " << i << " and “ << j << “.\n”; cout << " the product of them is " << i*j << ".\n"; return 0; } You can read multiple variables from the same line of keyboard input. They can be separated by any whitespace: a space, a tab or a newline. Again, for all the variables to be parsed, enter must be pressed.

Reading strings from keyboard input.

cin and strings // cin with strings #include using namespace std; int main () { string mystr; cout << "What's your name? "; cin >> mystr; cout << "Hello " << mystr << ".\n"; cout << "What is your favorite team? "; cin >> mystr; cout << "I like " << mystr << " too!\n"; return 0; } cin separates values with whitespace, so if you try to enter your full name this won’t work.

cin and strings // cin with strings #include using namespace std; int main () { string mystr; cout << "What's your name? "; getline(cin, mystr); cout << "Hello " << mystr << ".\n"; cout << "What is your favorite team? "; getline(cin, mystr); cout << "I like " << mystr << " too!\n"; return 0; } You can use the getline function to read the entire line entered before the user presses enter.

stringstream

// stringstreams #include using namespace std; int main () { string mystr; float price=0; int quantity=0; cout << "Enter price: "; getline (cin,mystr); stringstream(mystr) >> price; cout << "Enter quantity: "; getline (cin,mystr); stringstream(mystr) >> quantity; cout << "Total price: " << price*quantity << endl; return 0; } You can use a stringstream to move data into variables (just like cin).

File Streams

Writing to a file // basic file operations #include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; }

Writing to a file // basic file operations #include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; } We use an ofstream (output file stream) to read from a file.

Writing to a file // basic file operations #include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; } This opens the file for writing.

Writing to a file // basic file operations #include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; } Now instead of writing to the screen, we can write into that file.

Writing to a file // basic file operations #include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; } When we’re done, we need to close the file.

Writing to a file // basic file operations #include using namespace std; int main () { ofstream myfile; myfile.open("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; } However there is a problem. What if the “example.txt” file didn’t exist?

Writing to a file 2 // writing on a text file #include using namespace std; int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0; } We can check to see if a file was opened with the is_open() method.

Reading from a file // reading a text file #include using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline(myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; }

Reading from a file // reading a text file #include using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline(myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; } Reading from a file works just like reading from cin.

Reading from a file // reading a text file #include using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline(myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; } We use an ifstream (input file stream) to open a file for reading.

Reading from a file // reading a text file #include using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline(myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; } We can also check and see if the file continues to be good to read from (for example, we haven’t reached the end of the file).

Checking State Flags In addition to good(), which checks whether the stream is ready for input/output operations, other member functions exist to check for specific states of a stream (all of them return a bool value): bad() - 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. In order to reset the state flags checked by any of these member functions we have just seen we can use the member function clear(), which takes no parameters.

File Streams C++ provides the following classes to perform output and input of characters to/from files: 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.

Setting flags for file streams It is possible to set specific flags to change the way file streams read from and write to a file: 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 to any value, 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. This flag can only be used in streams open for output-only operations. ios::trunc - If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one. ofstream myfile; myfile.open ("example.bin", ios::out | ios::app | ios::binary); For example:

Default flags: class default mode parameters ofstreamios::out ifstreamios::in fstreamios::in | ios::out