20
Chapter 10 C++ for Java Programmers 1 C++ for Java Programmers Chapter 10 Input/Output

Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Embed Size (px)

Citation preview

Page 1: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 1

C++ for Java Programmers

Chapter 10

Input/Output

Page 2: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 2

Introduction Two competing I/O systems in C++.

Standard I/O Library, stdio Stream I/O Library, iostream

Never use both Libraries in the same program.

Page 3: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 3

The stdio Library “Inherited” from the C language. Widely known and available. Based on stable and well-exercised

libraries. Not as extendable or adaptable as the

newer Stream I/O Library. For developing new programs, the

Stream I/O Library is preferred.

Page 4: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 4

Examples of stdio# include <cstdio> // or stdio.hint c = getchar(); // read a single input character

putchar('x'); // print a single character

char * text = "please enter your name:";puts(text); // print the text string

char buffer[120];gets(buffer); // read a line of input

FILE * fp = fopen("mydata.dat", "r");if (fp == NULL)puts("file cannot be opened");

fputc('z', fp); // write character to fpint c = fgetc(fp); // read character from fpchar * msg = "unrecoverable program error";fputs(msg, stderr); // write message to standard error output

Page 5: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 5

Formatted Output The printf facility works only for

formatted primitive values, such as integers and floats.

Always verify that formatting commands match the argument types; compiler does not check

Page 6: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 6

Examples of Formatted Output

%d integer decimal value%o integer printed as octal%x integer printed as hexadecimal%c integer printed as character%u unsigned integer decimal value%f floating point value%g floating point value%s null terminated string value%% percent sign

Page 7: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 7

Exmples of printfint i = 3;int j = 7;double d = i / (double) j;printf("the value of %d over %d is %g", i, j, d);

char * fileName = ...;if (fopen(fileName, "r") == null)fprintf(stderr,"Cannot open file %s\n", fileName);

char buffer[180];sprintf(buffer, "the value is %d\n", sum);

double d = 23.5;printf("the value is %d\n", d); // error -- float printed as int

scanf("%d %f", &i, &f); // read an int, then a float

Page 8: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 8

Problems with printf Works fine for built-in types, but Not easily expanded for new user-

defined types Compiler cannot check

formatting/argument agreement So, is there another way??

Page 9: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 9

The Stream I/O Facility Whenever possible use the Stream I/O

Library rather than the Standard I/O Library.

Uses the ability to overload function names in C++.

Better possibilities for extendability as well as improved error detection.

Page 10: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 10

Stream output overloads << opostream & operator << (ostream & out, const int value){

// print signed integer values on a stream

unsigned int usvalue;if (value < 0) // print leading minus sign{

out << '-';usvalue = -value;

} else // print non-negative number

usvalue = value;

out << usvalue;return out;

}

Page 11: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 11

Printing int as recursive funinline char digitCharacter(unsigned int value) { return value + '0'; }

ostream & operator << (ostream & out, const unsigned int value)

{// print unsigned integer values on a stream

if (value < 10)out << digitCharacter(value);

else

{out << (value / 10); // recursive callout << digitCharacter(value % 10);

}return out;

}

Page 12: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 12

Complex output built in parts

# include <iostream>

cout << "n " << n << " m " << m << " average " << (n+m)/2.0 << '\n';

Page 13: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 13

Easy to extend to new types

ostream & operator << (ostream & out, const rational & value){

// print representation of rational number on an output streamout << value.numerator() << '/' << value.denominator();return out;

}

rational frac(3,4);cout << "fraction of " << 3 << " and " << 4 << " is “ << frac << endl;

Page 14: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 14

I/O Manipulators A manipulator is used to change features of the I/O

system.

ostream & endl (ostream & out) {

out << '\n'; // write the end of line character out.fflush(); // then flush the buffer return out; // then return the buffer

}

ostream & operator << (ostream & out, ostream & (*fun)(ostream &)){

return fun (out); // simply execute function}

Page 15: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 15

Stream Input Stream input ignores white space.

cin >> intval;

while (cin >> intval) {// process intval

...}// reach this point on end of input...

Visualize >> and << as arrows Input operator, >> x : points data into x Output operator, << x : copies data out of x

Page 16: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 16

File Streams A file stream is a stream that reads from or

writes to a file. Must include the header file

# include <fstream>

fstream header file includes the iostream header, so not necessary to include both.

The class ifstream and ofstream are used to create streams that are attached to input and output files.

Page 17: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 17

Conversion Operators A conversion operator changes a file stream into a

boolean value, whereby the value indicates the success or failure of the file opening operation.

char fileName = "outfile.dat";ofstream ofd(fileName); // create file for outputif (! ofd) {

cerr << " cannot open file " << fileName} else {

ofd << "first line in file"...

}

File operations in C++ throw far fewer exceptions than do in Java.

Page 18: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 18

Test your Understanding What is the include file for the standard

I/O library? How do you use the function printf? What are some problems with the printf

function?

Page 19: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 19

Test your Understanding What is the error?

double pi = 3.14159;

printf(“Pi is %d\n”, pi);

Page 20: Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output

Chapter 10 C++ for Java Programmers 20

Test your Understanding What is the include file for the stream

I/O facility? What operator is used for stream

output? Why is this approach more easily

extensible than the printf approach?