Lecture 2

advertisement
Lecture 2

Bookstore has ordered the real textbook.

Log into Linux, start a terminal


Has everyone successfully compiled and run a
program on Linux?
Change directory to cs215, create a
subdirectory lecture02 in it, and change to
that directory
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
1
Outline

More UNIX commands

Handling invalid input

Command-line arguments

File streams

Homework 1
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
2
More UNIX Commands

Copying files on local machine





cp <file(s) to be copied> <new name or directory>
If one file and a new name, a copy in current
directory
If multiple files and/or a directory, a copy with same
name
Example (note: last '.' is current directory):
cp /home/hwang/cs215/lecture02/*.* .
To copy directories, use -r (recursive) option:
cp ­r /home/hwang/cs215/lecture02 .
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
3
More UNIX Commands

Copying files on remote machine


scp - same syntax as cp
Remote name is prefixed with "remotehost:" if
username is the same or "username@remotehost:"
if username is not the same

Names are relative to remote user home directory

Example to copy from remote to local:
scp hwang@csserver.evansville.edu:/home/hwang/cs215/lecture02/*.* .

Example to copy from local to remote:
scp file.cpp hwang@csserver.evansville.edu:cs215/lecture02
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
4
More UNIX Commands

If you want to put files into a different directory,
you can move them. Works like cp. Also use
to rename.


mv <file(s) to be moved> <new name or directory>
To delete files, remove them

Deleting files: rm <files to be deleted>

Deleting directories (and their contents):
rm -r <directories>

cp, mv, and rm have -i (interactive) option
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
5
Handling Invalid Input


When an I/O stream is used in a boolean
context, it converts to true when the stream is
valid and false when the stream is invalid.
Example: checking for bad user input
cin >> anInt;
if (!cin) { cout << "Input error" << endl;}

Can combine this into one step since >> returns
the left-hand stream operand:
if (!(cin >> anInt)) { cout << "Input error" << endl; }
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
6
Handling Invalid Input

After invalid input has been detected, stream
must be cleared and offending input removed
from the stream. (Program in input.cpp)
bool valid; int value; string errorInput;
do {
cout << "Enter an integer: ";
if (cin >> value) { valid = true; }
else {
valid = false;
cin.clear(); // reset the stream
getline (cin, errorInput); // skip bad data
cout << "Bad input\n";
}
} while (!valid);
cout << "Entered value: " << value << endl;
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
7
Command-line Arguments



Most UNIX programs are not interactive
E.g. file names are given on command line and
are called command-line arguments
In C++ (and C), they are accessed using the
following function header for main:
int main (int argc, char *argv[])

This is the only other prototype allowed for the
main function. Parameter names are
conventional.
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
8
argc, argv

argc is the number of "words" on the command
line, including the command itself. argc would
be 5 for the following example:
g++ ­Wall ­o hello hello.cpp

argv is an array C-strings that are the "words".
Note that the command word is argv[0], so the
first argument is argv[1] and the last one is
argv[argc-1].
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
9
argc, argv

Always check if argc is the correct number
when it matters, and display a usage message.
Note the use of argv[0] for the command name.
if (argc != 3) {
cerr << "Usage: " << argv[0] << "input output" << endl;
exit (1);
} // end argc check

exit( ) is defined in library header <cstdlib>
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
10
File Streams



The main use for command-line arguments is to
pass names of files to programs rather than ask
for them interactively.
File streams are objects connected to files.
Two types are defined in system library
<fstream>:
ifstream inFileStream; // input file stream
ofstream outFileStream; // output file stream
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
11
Opening File Streams


Names of files must be C-strings (not C++
strings), e.g., argv elements
Attach physical file when variable is declared
(called explicit-value construction)
ifstream inFileStream(argv[1]);
ofstream outFileStream(argv[2]);

Use open( ) member function (i.e., a function
attached to the file stream object)
inFileStream.open(argv[1]);
outFileStream.open(argv[2]);
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
12
Opening File Streams

Should always check if file open (both input and
output files) was successful.
ifstream inFileStream(argv[1]);
if (!inFileStream) { // invalid stream
cerr << "Error opening file: " << argv[1] << endl;
exit(1);
} // end input file open check
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
13
Using File Streams



File streams are a kind of I/O stream
Insertion operator (<<) is used with ofstreams
to write to a file.
Extraction operator (>>) is used with ifstreams
to read from a file
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
14
Reading from a File

When an attempt is made to read past the end
of a file, the file stream becomes invalid. The
general pattern for reading from a file is:
// Open input file
ifstream inFileStream (argv[1]);
if (!inFileStream) { // error msg and exit }
// Read from file
itemType value; while (inFileStream >> value) {
// do something with value
} // end while valid input
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
15
Reading Whitespace


Program firstcopy.cpp attempts to implement
the cp UNIX command to copy a file by copying
one character at a time
Compile, run, and test
$ g++ ­Wall ­o firstcopy firstcopy.cpp
$ ./firstcopy test1.dat copytest1.dat
$ diff test1.dat copytest1.dat

UNIX command: diff - compare two files
character by character; no output if the same
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
16
Reading Whitespace

Try again
$ ./firstcopy test2.dat copytest2.dat
$ diff test2.dat copytest2.dat 

Problem: insertion operator (>>) skips
whitespace
Use get( ) member function. It takes a
character argument that is filled with the read
character. It also returns false if the stream
fails.
while (in.get(ch))
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
17
Closing Files

Close files using close( ) member function
inFileStream.close();
outFileStream.close();
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
18
Homework 1

Assignment is posted to course webpage

Due on Friday, preferably at beginning of class



cipher.cpp should be on the computer you will be
using in class. I.e., on csserver or your own laptop
It will be used to demonstrate electronic submission
system during class.
Hint: this program is almost exactly like the lecture's
file copy program. The only difference is that the
read character is (possibly) enciphered before it is
written to the output file.
Wednesday, January 12
CS 215 Fundamentals of Programming II - Lecture 2
19
Download