105 - Texas A&M University

Download Report

Transcript 105 - Texas A&M University

CSCE 121:509-512
Introduction to Program Design and Concepts
J. Michael Moore
Fall 2014
Set 11:
More Input and Output
Based on slides created by Bjarne Stroustrup and Jennifer Welch
1
Outline
•
•
•
•
•
Numeric Formatting
File Modes
Stringstreams
Reading a Line
Character Classification
CSCE 121:509-512
Set 11: More Input and Output
2
Integer Formatting Options
• Integer values:
– decimal (base 10), uses 0-9
• Ex: 1234 = 1*103 + 2*102 + 3*101 + 4*100
– octal (base 8), uses 0-7
• Ex: 2322 = 2*83 + 3*82 + 2*81 + 2*80
– hexadecimal (base 16), uses 0-9,a-f
• Ex: 4d2 = 4*162 + d*161 + 2*160 = 4*162 + 13*161 + 2*160
• Use manipulators to tell input and output
streams which base to use:
– dec, oct, or hex
– “sticky”: permanent
until changed again
CSCE 121:509-512
Set 11: More Input and Output
3
Integer Formatting Examples
int x = 1234;
cout << hex;
// from now on, ints will be written to screen in hex:
cout << x;
cin >> oct;
// from now on, ints will be read in from keyboard as
// if they are in octal
int y;
cin >> y;
cout << dec; // back to decimal output
cin >> dec;
// back to decimal input
CSCE 121:509-512
Set 11: More Input and Output
4
Floating-Point Output Formatting Options
• general (only in std_lib_facilities_4.h): iostream chooses best
format using n digits
– Ex: 1234.57
– use manipulator general
• scientific: one digit before decimal point, n digits after decimal
point, plus exponent
– Ex: 1.2345678e+03
– use manipulator scientific
• fixed: n digits after decimal point, no exponent
– Ex: 1234.567890
– use manipulator fixed
• n is the “precision” (coming up next)
CSCE 121:509-512
Set 11: More Input and Output
5
Precision
• For general, precision is the number of digits
• For scientific, precision is number of digits
after decimal point
• For fixed, precision is number of digits after
decimal point
• Use setprecision manipulator (for output
streams)
– Ex:
cout << scientific << setprecision(8);
CSCE 121:509-512
Set 11: More Input and Output
6
Still More Formatting Options
• You can control the width (number of
characters to be used for the output)
– not sticky
– output is never truncated to fit
• All this is the kind of stuff you look up when
you need it
CSCE 121:509-512
Set 11: More Input and Output
7
File Open Modes
Alternatives:
• for reading: ios_base::in
• for writing: ios_base::out
• append: ios_base::app
• binary: ios_base::binary
• and more (more stuff to look up)
Usage:
•
•
•
•
ofstream of1(name1); // default is ios_base::out
ifstream if1(name2); // default is ios_base::in
ofstream ofs(name,ios_base::app);
fstream fs(“myfile”,ios_base::in|ios_base::out);
// read and write; actually is the default for fstream
CSCE 121:509-512
Set 11: More Input and Output
8
Positioning in a filestream
• You can control where in the file you read and
write
– seekg (g for get, i.e., read)
– seekp (p for put, i.e., write)
• BUT, whenever you can, use simple streaming
– powerful abstraction
– try to just use istream and ostream
• Positioning is far more error-prone
– handling end of file is system-dependent
CSCE 121:509-512
Set 11: More Input and Output
9
Text vs. Binary Files
• Text file is a sequence of characters (fixed-size
units, each unit large enough to hold a char)
– to delimit values we use separation/termination
characters (e.g., space or tab)
• Binary file is a sequence of bits
– we use sizes to delimit values (e.g., if we are
reading an int followed by a double, there is no
blank space in between)
CSCE 121:509-512
Set 11: More Input and Output
10
Text vs. Binary
• Use text when you can
– human-readable
– easier to debug
– portable across systems
– most information can be represented reasonably
• Use binary when you must
– e.g., image files, sound files
– Look up how to do it when you need to do it
CSCE 121:509-512
Set 11: More Input and Output
11
Stringstreams
• A stringstream
– reads from a string (instead of a file or the keyboard)
– writes to a string (instead of a file or the screen)
double str_to_double(string s) {
// try to convert characters in s to a floating-point value
istringstream is(s); // input stringstream
double d;
is >> d; // access just like an istream
if (!is) error(“double format error”);
return d;
}
// ...
double d1 = str_to_double(“12.4”);
double d2 = str_to_double(“1.34e-3”);
double d3 = str_to_double(“twelve point three”); // error
CSCE 121:509-512
Set 11: More Input and Output
12
Stringstreams
• Input stringstreams (istringstream) are
useful for extracting typed objects out of
strings
• Output stringstreams (ostringstream) are
useful for formatting into a fixed-size space
(e.g., a box in a GUI)
– See textbook for ostringstream details;
analogous to ostream, supports <<
CSCE 121:509-512
Set 11: More Input and Output
13
Line-Oriented Input
Alternative approach to reading a series of
values from the keyboard one at at time
1.read the entire line into a string
string name; getline(cin,name);
2.convert the string into a stringstream
istringstream ss(name);
3.extract the values from the istringstream
ss >> first_name; ss >> second_name;
CSCE 121:509-512
Set 11: More Input and Output
14
Getting All the Characters
This way skips whitespace:
char ch;
while (cin >> ch) {
if (isalpha(ch)) { /* do something */ }
}
This way lets you see every character:
while (cin.get(ch)) {
if (isspace(ch)) { /* do something */}
else if (isalpha(ch)) {/* do something
else*/}
}
CSCE 121:509-512
Set 11: More Input and Output
15
An Aside
Useful functions that work on characters
• isspace(ch)
•
•
•
•
•
isalpha(ch)
isdigit(ch)
isupper(ch)
islower(ch)
isalnum(ch)
//
//
//
//
//
//
//
is
(‘
is
is
is
is
is
ch
’,
ch
ch
ch
ch
ch
whitespace?
‘\t\’, ‘\n’, etc.)
a letter? (a-z, A-Z)
a digit? (0-9)
an uppercase letter?
a lowercase letter?
a letter or a digit?
• etc.
CSCE 121:509-512
Set 11: More Input and Output
16
Line-Oriented Input Advice
• Avoid line-oriented input when you can
– i.e., use >> instead of getline()
• People often use getline() because they
see no alternative
– but it gets messy
– you often end up using >> anyway to parse the
values from a stringstream made out of the line
– you often end up having to use get() to read
individual characters
CSCE 121:509-512
Set 11: More Input and Output
17
Acknowledgments
• Photo on slide 1: “Cables Close Up” by
Michael Coghlan, licensed under CC BY-SA 2.0
• Slides are based on those for the textbook:
http://www.stroustrup.com/Programming/11_custom_io.ppt
CSCE 121:509-512
Set 11: More Input and Output
18