CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++...
Transcript of CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++...
![Page 1: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/1.jpg)
CIS 190: C/C++ Programming
Lecture 6
Introduction to C++
1
![Page 2: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/2.jpg)
Outline
• Changes for C++
– Files & Compiling
– Variables
– Functions
• Input/Output in C++
– cin/cout/cerr
– Print Functions
– Reading/Writing to Files
• hello_world.cpp
2
![Page 3: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/3.jpg)
Files in C++
• hello_world.c
3
![Page 4: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/4.jpg)
Files in C++
• hello_world.c
– becomes
• hello_world.cpp
4
![Page 5: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/5.jpg)
Files in C++
• hello_world.c
– becomes
• hello_world.cpp
• hello_world.h
5
![Page 6: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/6.jpg)
Files in C++
• hello_world.c
– becomes
• hello_world.cpp
• hello_world.h
– stays
• hello_world.h
6
![Page 7: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/7.jpg)
Compiling in C++
• instead of gcc use g++
• you can still use the same flags:
-Wall for all warnings
-c for denoting separate compilation
-o for naming an executable
-g for allowing use of a debugger
• and any other flags you used with gcc
7
![Page 8: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/8.jpg)
Outline
• Changes for C++ – Files & Compiling
– Variables
– Functions
• Input/Output in C++ – cin/cout/cerr
– Print Functions
– Reading/Writing to Files
• hello_world.cpp
8
![Page 9: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/9.jpg)
Variables in C++
• comments can be /* contained with asterisks */
or // all text after is a comment
• #define will still work
– but we can also use const instead
9
![Page 10: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/10.jpg)
#define vs const
• #define replaces with value at compile time
#define PI 3.14159265358979
int main()
{
printf(“Pi is %f\n”,
PI);
}
10
![Page 11: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/11.jpg)
#define vs const
• #define replaces with value at compile time
#define PI 3.14159265358979
int main()
{
printf(“Pi is %f\n”,
3.14159265358979);
}
11
![Page 12: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/12.jpg)
#define vs const
• const defines variable as unable to be changed
const double PI = 3.14159265358979;
• regardless of the choice, they are used the same way in code
area = PI * (radius * radius);
12
![Page 13: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/13.jpg)
Details about const
const double PI = 3.14159265358979;
• explicitly specify actual type
• a variable – so can be examined by debugger
• const should not be global
– very very rarely
– normally used inside classes
13
![Page 14: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/14.jpg)
Interacting with Variables in C
• in C, most of the variables we use are “primitive” variables (int, char, double, etc.)
• when we interact with primitive variables using provided libraries, we call functions and pass those variables in as arguments fopen(ifp, “input.txt”, “r”);
free(intArray);
strlen(string1);
14
![Page 15: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/15.jpg)
Interacting with Variables in C++
• in C++, many of the variables we use are instances of a class (like string, ifstream, etc.)
• when we want to interact with these variables, we use method calls on those variables inStream.open(“input.txt”);
string2.size();
15
![Page 16: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/16.jpg)
Using Variables in C++
• declaration is more lenient
– variables can be declared anywhere in the code
– may still want them at the top, for clarity
• C++ introduces new variables
– string
– bool
16
![Page 17: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/17.jpg)
string
• requires header file: #include <string>
Some advantages over C-style strings:
• length of string is not fixed
– or required to be dynamically allocated
• can use “normal” operations
• lots of helper functions
17
![Page 18: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/18.jpg)
Creating and Initializing a string
• create and initialize as empty
string name0;
18
![Page 19: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/19.jpg)
Creating and Initializing a string
• create and initialize as empty
string name0;
• create and initialize with character sequence string name1 (“Alice”);
string name2 = “Bob”;
19
![Page 20: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/20.jpg)
Creating and Initializing a string
• create and initialize as empty
string name0;
• create and initialize with character sequence string name1 (“Alice”);
string name2 = “Bob”;
• create and initialize as copy of another string string name3 (name1);
string name4 = name2;
20
![Page 21: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/21.jpg)
“Normal” string Operations
• determine length of string name1.size();
• determine if string is empty name2.empty();
• can use the equality operator
if (name1 == name2)
21
![Page 22: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/22.jpg)
More string Comparisons
• can also use the other comparison operators:
if (name1 != name2)
• alphabetically (but uses ASCII values) if (name3 < name 4)
if (name3 > name 4)
• and can concatenate using the ‘+’ operator
name0 = name1 + “ “ + name2;
22
![Page 23: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/23.jpg)
Looking at Sub-Strings
• can access one character like C-style strings name1[0] = ‘a’;
• can access a sub-string name1.substr(2,3);
• “ice” name2.substr(0,2);
• “Bo”
23
![Page 24: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/24.jpg)
bool
• two ways to create and initialize bool boolVar1 = true;
bool boolVar2 (false);
• can compare (and set) to true or false
24
![Page 25: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/25.jpg)
Outline
• Changes for C++
– Files & Compiling
– Variables
– Functions
• Input/Output in C++
– cin/cout/cerr
– Print Functions
– Reading/Writing to Files
• hello_world.cpp
25
![Page 26: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/26.jpg)
Functions in C++
• some similarity to functions in C
– variables are only in scope within the function
– require a prototype and a definition
– arguments can still be passed by reference or passed by value
• one small difference: no need to pass array length (can just use empty brackets) void PrintArray (int arr []);
26
![Page 27: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/27.jpg)
Using const in C++ functions
• when used on pass-by-value
int SquareNum (const int x) {
return (x * x); /* fine */
}
int SquareNum (int x) {
return (x * x); /* fine */
}
27
![Page 28: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/28.jpg)
Using const in C++ functions
• when used on pass-by-value
• no real difference; kind of pointless
– changes to pass-by-value variables don’t last beyond the scope of the function
• conventionally: not “wrong,” but not done
28
![Page 29: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/29.jpg)
Using const in C++ functions
• when used on pass-by-reference
void SquareNum (const int *x) {
(*x) = (*x) * (*x); /* error */
}
void SquareNum (int *x) {
(*x) = (*x) * (*x); /* fine */
}
29
![Page 30: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/30.jpg)
Using const in C++ functions
• when you compile the “const” version:
void SquareNum (const int *x) {
(*x) = (*x) * (*x); /* error */
}
error: assignment of read-only
location '*x'
30
![Page 31: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/31.jpg)
Using const in C++ functions
• when used on pass-by-reference
• prevents changes to variables, even when they are passed in by reference
• conventionally:
– use for user-defined types (structs, etc.)
– don’t use for simple built-in types (int, float, char)
• except maybe arrays
31
![Page 32: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/32.jpg)
Outline
• Changes for C++ – Files & Compiling
– Variables
– Functions
• Input/Output in C++ – cin/cout/cerr
– Print Functions
– Reading/Writing to Files
• hello_world.cpp
32
![Page 33: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/33.jpg)
Working with Input/Output in C++
• at top of each file that uses input/output
using namespace std;
• to use streams to interact with user/console, must have #include <iostream>
• to use streams to interact with files, must have #include <fstream>
33
![Page 34: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/34.jpg)
Input/Output in C++
#include <stdio.h>
printf(“test: %d\n”, x);
scanf(“%d”, &x);
34
![Page 35: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/35.jpg)
Input/Output in C++
#include <stdio.h>
#include <iostream>
printf(“test: %d\n”, x);
scanf(“%d”, &x);
35
![Page 36: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/36.jpg)
Input/Output in C++
#include <stdio.h>
#include <iostream>
using namespace std;
printf(“test: %d\n”, x);
scanf(“%d”, &x);
36
![Page 37: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/37.jpg)
Input/Output in C++
#include <stdio.h>
#include <iostream>
using namespace std;
printf(“test: %d\n”, x);
cout << “test: ” << x << endl;
scanf(“%d”, &x);
37
![Page 38: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/38.jpg)
Input/Output in C++
#include <stdio.h>
#include <iostream>
using namespace std;
printf(“test: %d\n”, x);
cout << “test: ” << x << endl;
scanf(“%d”, &x);
cin >> x;
38
![Page 39: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/39.jpg)
The << Operator
• insertion operator; used along with cout
• separate each “type” of thing we print out
int x = 3;
cout << “X is: ” << x
<< “; squared ”
<< SquareNum(x) << endl;
39
![Page 40: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/40.jpg)
The << Operator
• insertion operator; used along with cout
• separate each “type” of thing we print out
int x = 3;
cout << “X is: ” << x
<< “; squared”
<< SquareNum(x) << endl;
40
![Page 41: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/41.jpg)
The >> Operator
• extraction operator; used with cin
– returns a boolean for (un)successful read
• like scanf and fscanf, skips leading whitespace, and stops reading at next whitespace
• don’t need to use ampersand on variables cin >> firstName >> lastName >> age;
41
![Page 42: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/42.jpg)
using namespace std
• at top of each file you must have using namespace std;
• otherwise you must use instead of std::cin cin
std::cout cout
std::endl endl
42
![Page 43: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/43.jpg)
cerr
• in addition to cin and cout, we also have a stream called cerr
• use it instead of stderr:
fprintf(stderr, “error!\n”);
43
![Page 44: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/44.jpg)
cerr
• in addition to cin and cout, we also have a stream called cerr
• use it instead of stderr:
fprintf(stderr, “error!\n”);
cerr << “error!” << endl;
44
![Page 45: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/45.jpg)
Outline
• Changes for C++
– Files & Compiling
– Variables
– Functions
• Input/Output in C++
– cin/cout/cerr
– Print Functions
– Reading/Writing to Files
• hello_world.cpp
45
![Page 46: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/46.jpg)
Quick Note on “Print” Functions
two basic ways to handle printing:
• function returns a string
• function performs its own printing
46
![Page 47: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/47.jpg)
Quick Note on “Print” Functions
two basic ways to handle printing:
• function returns a string
– call function within a cout statement
string PrintName (int studentNum);
• function performs its own printing
47
![Page 48: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/48.jpg)
Quick Note on “Print” Functions
two basic ways to handle printing:
• function returns a string
– call function within a cout statement
string PrintName (int studentNum);
• function performs its own printing
– call function separately from a cout statement
void PrintName (int studentNum);
48
![Page 49: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/49.jpg)
Outline
• Changes for C++
– Files & Compiling
– Variables
– Functions
• Input/Output in C++
– cin/cout/cerr
– Print Functions
– Reading/Writing to Files
• hello_world.cpp
49
![Page 50: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/50.jpg)
Reading In Files in C++
FILE *ifp;
read/write will be specified in call to fopen()
50
![Page 51: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/51.jpg)
Reading In Files in C++
FILE *ifp;
ifstream inStream;
read specified by variable type
– ifstream for reading
51
![Page 52: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/52.jpg)
Reading In Files in C++
FILE *ifp;
ifstream inStream;
ifp = fopen(“testFile.txt”, “r”);
read is specified by “r” in call to fopen
52
![Page 53: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/53.jpg)
Reading In Files in C++
FILE *ifp;
ifstream inStream;
ifp = fopen(“testFile.txt”, “r”);
inStream.open(“testFile.txt”);
read is specified by declaration of inStream as a variable of type ifstream; used by open()
53
![Page 54: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/54.jpg)
Reading In Files in C++
FILE *ifp;
ifstream inStream;
ifp = fopen(“testFile.txt”, “r”);
inStream.open(“testFile.txt”);
if ( ifp == NULL ) { /* exit */ }
54
![Page 55: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/55.jpg)
Reading In Files in C++
FILE *ifp;
ifstream inStream;
ifp = fopen(“testFile.txt”, “r”);
inStream.open(“testFile.txt”);
if ( ifp == NULL ) { /* exit */ }
if (!inStream) { /* exit */ }
55
![Page 56: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/56.jpg)
Reading In Files in C++
• ifstream inStream;
– declare an input file variable
• inStream.open(“testFile.txt”);
– open a file for reading
• if (!inStream) { /* exit */ }
– check to make sure file was opened
56
![Page 57: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/57.jpg)
Writing to Files in C++
• very similar to reading in files
• instead of type ifstream, use type ofstream
• everything else is the same
57
![Page 58: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/58.jpg)
Writing To Files in C++
• ofstream outStream;
– declare an output file variable
• outStream.open(“testFile.txt”);
– open a file for writing
• if (!outStream) { /* exit */ }
– check to make sure file was opened
58
![Page 59: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/59.jpg)
Opening Files
• the .open() call for file streams takes a char* (a C-style string)
• if you are using a C++ string variable, you must give it a C-style string
• calling .c_str() will return a C-style string cppString.c_str()
stream.open(cppString.c_str() );
59
![Page 60: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/60.jpg)
Using File Streams in C++
• once file is correctly opened, use your ifstream and ostream variables the same as you would use cin and cout
inStm >> firstName >> lastName;
outStm << firstName << “ “
<< lastName << endl;
60
![Page 61: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/61.jpg)
Advantages of Streams
• does not use placeholders (%d, %s, etc.)
– no placeholder type-matching errors
• can split onto multiple lines easily
• precision with printing can be easier
– once set using setf(), the effect remains until changed with another call to setf()
61
![Page 62: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/62.jpg)
Finding EOF with ifstream – Way 1
• use >>’s boolean return to your advantage
while (inStream >> x)
{
// do stuff with x
}
62
![Page 63: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/63.jpg)
Finding EOF with ifstream – Way 2
• use a “priming read”
inStream >> x;
while( !inStream.eof() )
{
// do stuff with x
// read in next x
inStream >> x;
}
63
![Page 64: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/64.jpg)
Outline
• Changes for C++
– Files & Compiling
– Variables
– Functions
• Input/Output in C++
– cin/cout/cerr
– Print Functions
– Reading/Writing to Files
• hello_world.cpp
64
![Page 65: CIS 190: C/C++ Programmingcis190/fall2014/lectures/06/lec06.pdf · •Input/Output in C++ –cin/cout/cerr –Print Functions –Reading/Writing to Files •hello_world.cpp 45. Quick](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff78b8423852968201ae8d7/html5/thumbnails/65.jpg)
hello_world.cpp
/* let’s convert this to use
streams and C++’s library */
#include <stdio.h>
int main() {
printf(“Hello world!\n”);
return 0;
}
65 LIVECODING LIVECODING