Moving Data Within a C++ Program
-
Upload
tarik-chandler -
Category
Documents
-
view
21 -
download
0
description
Transcript of Moving Data Within a C++ Program
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
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
Review: C++ Input/Output Stream Classes
#include <iostream>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;}
• <iostream> 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 <fstream>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;}
• <fstream> 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 <iostream>#include <string>using namespace std;int main (int, char*[]) { string s; // empty s = “”; // empty s = “hello”; s += “, ”; s = s + “world!”; cout << s << endl; return 0;}
• <string> header file• Various constructors• Assignment operator• Overloaded operators
+= + < >= == []• The last one is really
useful: indexes stringif (s[0] == ‘h’) …
CSE 232: Moving Data Within a C++ Program
Review: C++ String Stream Classes
#include <iostream>#include <fstream>#include <sstream>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;}
• <sstream> 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 <string>#include <cstring>#include <sstream>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