1 / 16

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

Télécharger la présentation

Moving Data Within a C++ Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. C++ Reference Link on CSE232 Web Page

  4. Main C++ Reference Page

  5. C++ I/O Page

  6. C++ I/O Examples Page

  7. 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?

  8. C++ I/O Display All

  9. #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 Review: C++ Input/Output Stream Classes

  10. #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 Review: C++ File I/O Stream Classes

  11. 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

  12. #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 string if (s[0] == ‘h’) … Review: C++ string Class

  13. #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 Review: C++ String Stream Classes

  14. #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 Using C++ String Stream Classes

  15. 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

  16. 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

More Related