Title: Streaming and I0
1Streaming and I/0
2Menu
- Reading/writing text file streams
- Reading/writing string streams
- Command line arguments
3Reading/writing text streams
- All programs weve written so far have read input
from standard input, and written output to
standard output. - cin, cout, and cerr are C streams.
- We will now extend this concept to disk files.
4- To access a disk file, we can open a file stream
variable. - include ltiostreamgt
- ifstream infile(filename) // input
- ofstream outfile(filename) // output
- fstream inoutfile(filename) // in/output
5- Example (code fragment)
- include ltiostreamgt
- ...
- ifstream input_data
- input_data.open(myfile)
- int n
- double x
- input_data gtgt n gtgt x
- ...
6- Reading strings from streams (code fragment)
- include ltiostreamgt
- ...
- ifstream input_data
- input_data.open(myfile)
- string s
- input_data gtgt s // read word
- getline(input_data,s)
- ...
7- Reading characters from streams (code fragment)
- include ltiostreamgt
- ...
- ifstream input_data
- input_data.open(myfile)
- char ch
- input_data.get(ch) // get one character
- ...
8- Some other member functions of input streams
- input_stream.peek(ch) // look ahead one char
- input_stream.unget() // put the last char back
- Some member functions common to both input and
output streams - input_stream.fail() // stream op failed
- input_stream.eof() // end of stream reached
- input_stream.close()
- input_stream.open(filename)
9- Code fragment
- char ch
- ifstream input_stream
- input_stream.open(mydata)
- input_stream.get(ch) // get one character
- if(0lt ch and ch lt 9)
-
- input_stream.unget()
- int n
- input_stream gtgt n
-
- input_stream.close()
10- Output file streams (code fragment)
- include ltiostreamgt
- ...
- ofstream output_data(myfile)
- output_data ltlt n ltlt _ ltlt x ltlt \n
- output_data.put(ch) // write a single character
- output_data.close()
- ...
11- Many compilers do not support string parameters
to the open/close file function. - ...
- string s myfile
- ofstream output_data
- output_data.open(s) // sometimes supported
- ...
- output_data.open(s.c_str())
- ...
- member function s.c_str() yields standard C
character array
12- Example paths as strings in Unix
- ...
- string s /home/ugrad/hacker/myfile.dat
- s hacker/myfile.dat // file in home
directory - s myfile.dat // file in current directory
- On PC
- string s C\\hacker\\myfile.dat
- // corresponds with file C\hacker\myfile.dat
13File paths
- User could be prompted for a file name
- cout ltlt Type filename
- cin gtgt s
- File name could be computed or concatenated
- string homedir
- s homedir myfile dat
-
14String Streams
- include ltsstreamgt
- istringstream reads from a string
- ostringstream writes to a string
- Has same interfaces as other stream classes, but
allows read/write to a string object.
15- Code fragment
- ...
- string input January 23, 1955
- istringstream instr(input) // constructor
- string month,comma
- int day,year
- instr gtgt month gtgt day gtgt comma gtgt year
- ...
- C does the conversion from string to the other
types.
16- include ltsstreamgt
- int string_to_int(string s)
-
- istringstream instr(s) // constructor
- int n
- instr gtgt n
- return n
17- include ltsstreamgt
- string double_to_string(double a)
-
- ostringstream outstr // constructor
- outstr ltlt a
- return outstr.str() // return string equiv.
18- include ltsstreamgt
- string double_to_string(double a)
-
- ostringstream outstr // constructor
- outstr ltlt setprecision(5) // 5 places after
decimal - outstr ltlt a
- return outstr.str() // return string equiv.
-
- // double_to_string(sqrt(2)) returns 1.41421
19Command Line Arguments
- There are different ways to start a program
- click on icon
- type name of program and return
- the latter is called invoking the program from
the command line.
20Command Line Arguments
- You can include additional arguments on the
command line e.g., in Unix - ls -l
- make main
- g -Wall -ansi main.cpp -o main
- Usually, we interpret symbols preceded by - as
options.
21Arguments to C main function
- int main(int argc, char argv)
-
- ...
-
22- Example command line
- myprog -v input.dat
- In this case
- argc 3
- string(argv0) myprog
- string(argv1) -v
- string(argv2) input.dat
- argv0 always gives the name of the program
- argc always gt 1
23- Number of command line arguments can vary
- myprog -v input.dat
- myprog
- myprog -i -v input.dat -o output.dat
- argv0 always gives the name of the program
- argc always gt 1
- What is the value of argc in each of the cases
above??
24Caesars Encryption Algorithm
- Also known as Caesars Cipher.
- Scrample message via algorithm
- specify integer key k between 0 and 25
- to encrypt, shift each input character by k
- Example
- k 3, input ABCDEFG
- output DEFGHIJ
- To decrypt message, use key -k
-
25Program arguments
- optional -d flag indicates decryption rather than
encryption - optional encryption key -kltintgt
- input file name
- output file name
- Examples
- crypt -k11 input.txt encrypt.txt
- crypt -d -k11 encrypt.txt output.txt
26functions used
- usage(string program_name)
- open_file_error(string filename)
- remainder(int a, int n)
- encrypt(char ch, int k)
- encrypt_file(ifstream in, ofstream out, int k)
- string_to_int(string s)
- main(int argc, char argv)
27Main if argc lt 3 or argc gt 5 usage(string(argv
0)) exit for all command line
arguments if -d option, then set decrypt if
-kn option, set keyn, otherwise key3 if not
option, input filename or output
filename openfile (either infile or
outfile) if(nfile ! 2) usage(string(argv0))
exit if(decrypt) key -key encrypt_file(in
file,outfile,key) infile.close() outfile.close
() done
28void usage(string program_name) / PURPOSE
Prints usage instructions RECEIVES
program_name - the name of this program /
cout ltlt "Usage " ltlt program_name ltlt " -d
-kn infile outfile\n" exit(1)
29void open_file_error(string filename) / PURPOSE
Prints file opening error message RECEIVES
filename - the name of the file that could
not be opened / cout ltlt "Error opening file "
ltlt filename ltlt "\n" exit(1)
30 void encrypt_file(ifstream in, ofstream out,
int k) / PURPOSE Encrypt a file using the
Caesar cipher RECEIVES in - the file to read
from out- the file to write to
k - the encryption key / char ch
while (in.get(ch)) out.put(encrypt(ch,
k))
31 char encrypt(char ch, int k) / PURPOSE
Encrypt a character using the Caesar cipher
RECEIVES ch - the character to encrypt
k - the encryption key RETURNS the
encrypted character / const int NLETTER
26 if ('A' lt ch ch lt 'Z') return
static_castltchargt('A' remainder(ch - 'A' k,
NLETTER)) if ('a' lt ch ch lt 'z')
return static_castltchargt('a' remainder(ch - 'a'
k, NLETTER)) return ch
32int remainder(int a, int n) / PURPOSE Compute
correct remainder for negative dividend
RECEIVES a - an integer n - an
integer gt 0 RETURNS the mathematically
correct remainder r such that a - r
is divisible by n and 0 lt r and r lt n / if
(a gt 0) return a n else return
n - 1 - (-a - 1) n
33 int string_to_int(string s) / PURPOSE Convert
a string to an integer, e.g. "3" -gt 3
RECEIVES s - a string representing an integer
RETURNS the equivalent integer /
istringstream instr(s) int n instr gtgt n
return n
34 int main(int argc, char argv) bool decrypt
false int key 3 int nfile 0 / the
number of files specified / ifstream infile
ofstream outfile if (argc lt 3 or argc gt 5)
usage(string(argv0))
35for (int i 1 i lt argc i) string arg
string(argvi) if (arg.length() gt 2 and
arg0 '-') / it is a command line
option / char option arg1
if (option 'd') decrypt true
else if (option 'k') key
string_to_int(arg.substr(2, arg.length() - 2))
36else nfile if (nfile 1)
infile.open(arg.c_str())
if (infile.fail()) open_file_error(arg)
else if (nfile 2)
outfile.open(arg.c_str()) if
(outfile.fail()) open_file_error(arg)
37if(nfile ! 2) usage(string(argv0)) if
(decrypt) key -key encrypt_file(infile,
outfile, key) infile.close()
outfile.close() return 0 // end of main