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.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 3: Input/Output
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.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
CSCE 121: Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne.
C++ Programming Certificate University of Washington Cliff Green
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.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Ch 10. Input/Output1 Ch. 10 Input/Output Oregon State University Timothy Budd.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
C++ Streams Lecture-2.
File I/O ifstreams and ofstreams Sections 11.1 &
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
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.
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.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
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.
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.
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
Chapter 3: Input/Output
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in.
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.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
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.
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
Learners Support Publications Working with Files.
Lecture 14 Arguments, Classes and Files. Arguments.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
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.
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.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Basic concepts of C++ Presented by Prof. Satyajit De
Introduction to C++ (Extensions to C)
ifstreams and ofstreams
Command Line Arguments
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
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.
COMP 2710 Software Construction File I/O
Friday, January 19, 2018 Announcements… For Today… For Next Time…
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Chapter 2 part #3 C++ Input / Output
Standard Input/Output Streams
Standard Input/Output Streams
File I/O Streams, files, strings 1 1.
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
Chapter 3: Input/Output
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O.
Chapter 2 part #3 C++ Input / Output
ifstreams and ofstreams
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

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 from the standard input stream –Getting data from files Output –Sending data to standard output (we’ve looked at this) –Sending data to files Transfer within the program –Moving data into and out of different types of variables –Moving data into and out of different data structures

CSE 232: Moving Data Within a C++ Program Overview of Today’s Session Using the on-line C++ reference pages (throughout) Basic input and output stream features Basic file input and output stream features Moving data into and out of variables Moving character data into and out of strings Moving data into and out of a vector container

CSE 232: Moving Data Within a C++ Program C++ Reference Link on CSE232 Web Page

CSE 232: Moving Data Within a C++ Program Main C++ Reference Page

CSE 232: Moving Data Within a C++ Program C++ I/O Page

CSE 232: Moving Data Within a C++ Program C++ I/O Examples Page

CSE 232: Moving Data Within a C++ Program File I/O Examples Exercise: try out examples from C++ I/O reference –Do they work as written? –What files do you need to include to make them work? –What happens if you try to open a file that doesn’t exist? –What other ways can you explore the behaviors of the features those examples are using/illustrating?

CSE 232: Moving Data Within a C++ Program C++ I/O Display All

CSE 232: Moving Data Within a C++ Program Review: C++ Input/Output Stream Classes #include using namespace std; int main (int, char*[]) { int i; // cout == std ostream cout << “how many?” << endl; // cin == std istream cin >> i; cout << “You said ” << i << “.” << endl; return 0; } header file –Use istream for input –Use ostream for output Overloaded operators << ostream insertion operator >> istream extraction operator Other methods –ostream: write, put –istream: get, eof, good, clear Stream manipulators –ostream: flush, endl, setwidth, setprecision, hex, boolalpha

CSE 232: Moving Data Within a C++ Program Review: C++ File I/O Stream Classes #include using namespace std; int main () { ifstream ifs; ifs.open (“in.txt”); ofstream ofs (“out.txt”); if (ifs.is_open () && ofs.is_open ()) { int i; ifs >> i; ofs << i; } ifs.close (); ofs.close (); return 0; } header file –Use ifstream for input –Use ofstream for output Other methods –open, is_open, close –getline –seekg, seekp File modes –in, out, ate, app, trunc, binary

CSE 232: Moving Data Within a C++ Program Redirecting File Output Exercise: printing to a file vs. to stdout –Use the standard syntax for main that we used last week –Program always writes out “hello, world!” –If argc > 1 writes to file whose name is given by argv[1] –Otherwise writes to standard output

CSE 232: Moving Data Within a C++ Program Review: C++ string Class #include using namespace std; int main (int, char*[]) { string s; // empty s = “”; // empty s = “hello”; s += “, ”; s = s + “world!”; cout << s << endl; return 0; } header file Various constructors Assignment operator Overloaded operators += + = == [] The last one is really useful: indexes string if (s[0] == ‘h’) …

CSE 232: Moving Data Within a C++ Program Review: C++ String Stream Classes #include using namespace std; int main () { ifstream ifs (“in.txt”); if (ifs.is_open ()) { string line_1, word_1; getline (ifs, line_1); istringstream iss (line_1); iss >> word_1; cout << word_1 << endl; } return 0; } header file –Use istringstream for input –Use ostringstream for output Useful for scanning input –Get a line from file into string –Wrap string in a stream –Pull words off the stream Useful for formatting output –Use string as format buffer –Wrap string in a stream –Push formatted values into stream –Output formatted string to file

CSE 232: Moving Data Within a C++ Program Using C++ String Stream Classes #include using namespace std; int main (int argc, char *argv[]) { if (argc < 3) return 1; ostringstream argsout; argsout << argv[1] << “ ” << argv[2]; istringstream argsin (argsout.str()); float f,g; argsin >> f; argsin >> g; cout << f << “ / ” << g << “ is ” << f/g << endl; return 0; } Program gets arguments as C-style strings But let’s say we wanted to input floating point values from the command line Formatting is tedious and error-prone in C-style strings ( sprintf etc.) iostream formatting is friendly Exercise: check whether any of the strings passed by argv are unsigned decimal integers (leading zeroes still ok) –print their sum if there are any –otherwise print the value 0

CSE 232: Moving Data Within a C++ Program In-Memory “(in core)” Formatting Exercise: read and translate a file –Read integers and booleans (“true”, “false”) from a file Test by writing your own test file with different combinations –Use a string to get one line of data at a time –Use a string stream to extract space separated tokens into another string variable –Check whether each token is a boolean (if not treat as int) Convert to local variable of that type using another string stream Printout whether it’s a boolean or an integer, and print out the value of the local variable, making sure to preserve formatting

CSE 232: Moving Data Within a C++ Program A Couple More Things to Try Exercise: printing out text from a named file –Open a text file whose name is given in argv –Print out the contents of the file to standard output –Detect the end of the file Stop reading text, close the named file, and end the program Exercise: typing text into a named file –Read text from the standard input stream –Put the text into a file whose name is given by argv[1] –Detect when the user types in the character sequence q! Stop reading text, close the named file, and end the program