Laboratory 3: Reading & Writing to Text and Binary files and Bitwise
Manipulation of a Byte
In this
lab, you will learn how to read and write in C++ using different methods,
and observe the difference between writing into a file in text fmode
and in binary mode.
You will also learn how
to manipulate bits of a byte (unsigned char), needed in A1.
-
Topics to be explained by your TA.
-
Manipulating bits of a byte, stored in an unsigned char
-
unsigned char
data type
-
bitwise operators:
| (or), & (and), ~ (not), ^ (xor), << (left shif), >>
(right shift)
-
Checking the state of a file-stream ( using eof(),
good(), bad(),
fail() )
-
Different ways of reading a file
-
character-by-character (using get() or >>)
-
reading standard data types from a text file (using >>)
-
line-by-line ( using getline()
)
-
length-wise ( using get()
)
-
reading up to a delimiter (getline()
or get() )
-
reading one or more characters (using read())
-
Different ways of writing into file
-
character-by-character (using put() or <<)
-
writing standard data types from a text file (using <<)
-
line-by-line ( using <<)
-
writing one or more characters (using write())
YOUR TASK
Programs for bit manipulation and I/O operations:
-
Create a C++ program "lab3compress" that prompts the user for 4 characters
which should be characters in {A,C,G,T} and creates a byte containing an
encoding of this sequence of 4 characters where characters are substituted
for the following bits: (A:00, C:01, G:10, T:11). This template program
already contains some sample usage of the bitwise operations, the prompting/input
and the output of the byte. Your task is to fill the code that sets the
bits of the unsigned char (1byte).
-
Create a project called "lab3compress" containing the following file: lab3compress.cpp
-
Run your project, observing the use of bitwise operations.
-
Add your code for creating the encoding of the 4 characters into a byte.
-
Test your program with several input characters to see if your code is
correct.
-
Experiment with the program "lab3IOsamples.cpp" that manipulates I/O streams
using different methods.
-
Create a project called "lab3IOsamples" containing the following file: lab3IOsamples.cpp
-
Dowload the following input file: lowcomp1.txt
-
Try to guess the results of the code before running it.
-
Run your program and verify the results (screen output and created files).
-
If you wish, modify this program in order to try similar operations with
the files.
NO SOLUTION PROVIDED
We will not provide solutions for lab3compress, as it is part of A1.