F3. For each of the commands discussed above which don't result
in an error code situation, there are
file IO operation/s that need to be completed in addition to
outputting a correctly formatted string to the
console. The console string output will require you to implement a
binary to decimal algorithm, whereas the
the file IO operations will require you to call the provided
functions correctly. Your solution should
call the provided File IO function/s by passing the correct
arguments as well as display the correctly
formatted console output for each situation.
Note: You do not need to modify the provided FileWriter() and
FileReader() function implementations in order
to use them. It is possible to score 100% by just using them as
provided. If you choose to not call the
File IO functions you can still achieve some marks by outputting
the required (i.e. correctly formatted)
console output for all error code situations and the OUSB read and
write based commands outlined below.
Note: Section F3.1 and F3.2 will discuss the OUSB write based
command requirements and Section F3.3 and
F3.4 will discuss the OUSB read based requirements.
- Short description of provided FileWriter() function:
Prototype:
errno_t
FileWriter(const char* filename, const char* strToWrite);
Parameters:
filename - Null-terminated string that
holds the name of the file to work on.
strToWrite
- Null-terminated source string that will be written to the
first line of the file.
Return
Value - Zero if successful; an error code on failure.
- Short description of provided FileReader() function:
Prototype:
errno_t
FileReader(const char* filename, char* resultStr, int byteCount =
maxByteSize);
Parameters:
filename - Null-terminated string that
hold the name of the file to work on.
resultStr
- Location of the destination string
(null-terminated).
byteCount - Used to limit the number of
bytes read in from the file (default = maxByteSize)
Return
Value - Zero if successful; an error code on failure.
Note: As part of section F3, you will also be required to output a
very specifically formatted string to
the console under normal operating conditions. The string will
consist of two tokens. The first token will
be and eight (8) or ten (10) character string that represents an
8-bit or 10-bit binary number followed by a
comma character and a space (i.e. ", "), which is then followed by
the second token. The second token is the
decimal value of the binary value in the first token. Under some
situations the autotester will test these
tokens separately, however they must be correctly formatted and on
the same console output line.
______ Write: "portb.txt" file instructions:
---------------------------------------------
F3.1 If there is a valid fourth input argument passed to your
solution, and the first, second and third
input arguments are "-b", "io" and "portb", respectively, your
solution should call the FileWriter()
function with the first argument of the FileWriter() function being
set to "portb.txt" and the second
argument as a reference to the null terminated character array
containing the 8-bit binary number
(i.e. the fourth input argument). The 8-bit binary representation
should be in the conventional form
(e.g. b7,b6,b5,b4,b3,b2,b1,b0), with the MSB being the left-hand
most value and the LSB (bit-0)
as the most right-hand value.
F3.1.1 If no error was returned by the FileWriter() function,
your solution should display the 8-bit
binary representation of the input value to the console followed by
a comma character and a space
(i.e. ", "), which is then followed by the decimal value. As an
example, the exact format of console
output for a value of "01000000" (i.e. 64 in decimal) as the fourth
input argument would be:
01000000, 64
No other characters should be displayed to the console window.
Note: The first token displayed to the console output (i.e. the
8-bit binary number string) must be a string
of eight characters with each character either being a '0' (zero)
or a '1' one. The MSB (i.e. bit-7) will be
the left-hand most value and the LSB (i.e. bit-0) will be the most
right-hand value. The second token that
comes after comma and space (ie. ", ") will require you to develop
an algorithm to convert binary to decimal.
Under some situations the Autotester will test these tokens
separately, however they must be correctly
formatted and on the same console output line. The same will apply
to other sections in F3.
------------------------------------------------------------------------------------------------------------
Hint: One possible way to convert binary to decimal is to test each
position of the binary string for '1'
and summate its contribution by calculating 2 to the power of its
index position - see slide 4 of lecture 2.
e.g. the binary string 0110 (which is 6 decimal) can be
converted by calculating the sum of: 2^2 + 2^1 = 6
------------------------------------------------------------------------------------------------------------
F3.1.2 If no errors occurred, then your solution should return 0
and exit. Else, if an error occurred during
any part of the process, nothing other than the appropriate error
code should be displayed to the console.
Any error associated with the file IO operation (i.e. the provided
FileWriter() function) should be treated
as a MURPHY'S LAW error and nothing other than 'Z' should be
displayed to the console. Your solution should
always return 0 and exit.
Example 9:
- Your code is executed with following arguments:
your_code.exe -b io portb 00100000
- Your code should determine that it is required to convert
the binary number (0b00100000) to 32 decimal
(i.e. 2^5 = 23).
- It should then call the FileWriter() with the correct
arguments so that the portb.txt file contains the
string: 00100000
- If no errors occur, it should then print the specified
output F3.1.1 to the console. Hence, nothing other
than the following string should be displayed to the
console):
00100000, 32
- Then your code will return 0; and exit.
- You should then manually check that the portb.txt file
holds the string "00100000" as its first line in
the file.
______ Write: "portd.txt" file instructions:
---------------------------------------------
F3.2 If there is a valid fourth input argument passed to your
solution, and the first, second and third
input arguments are "-b", "io" and "portd", respectively, your
solution should call the FileWriter()
function with the first argument of the FileWriter() function being
set to "portd.txt" and the second
argument as a reference to the null terminated character array
containing the 8-bit binary representation
of the value that should be written to the portd.txt file with bits
7 and 2 set to '0' (zero). Hence, if
"11111111" was the fourth input argument (i.e. which is 255 in
decimal), the final string that will be
written to the portd.txt file must become "01111011" (i.e. 123 in
decimal).
F3.2.1 If no error was returned by the FileWriter() function,
your solution should display the 8-bit
binary representation of the input value to the console followed by
a comma character and a space
(i.e. ", "), which is then followed by the decimal value of the
original binary input argument. As an
example, the exact format of console output for a value of 11111111
as the fourth input argument would be:
255, 11111111
No other characters should be displayed to the console window.
Note: The console output should retain bits 7 and 2 in all cases
(i.e. The console output should not have
bit 7 and 2 should cleared) as this is how the real OUSB Board and
ousb.exe interface application deals
with such situations in the real world. Only the string written to
the file will have bit 7 and 2 cleared.
F3.2.2 If no errors occurred, then your solution should return 0
and exit. Else, if an error occurred during
any part of the process, nothing other than the appropriate error
code should be displayed to the console.
Any error associated with the file IO operation (i.e. the provided
FileWriter() function) should be treated
as a MURPHY'S LAW error and nothing other than 'Z' should be
displayed to the console. Your solution should
always return 0 and exit.
Example 10:
- Your code is executed with following arguments:
your_code.exe -b io portd 01111111
- Your code should determine that it is required to convert
the binary number (0b01111111) to 127 decimal.
- It should then call the FileWriter() with the correct
arguments so that the portd.txt file contains the
string: 01111011
NOTE: Bit 2 was required to be cleared and set to 0.
Bit 7 in this example was already set to 0.
- If no errors occur, it should then print the specified
output F3.2.1 to the console. Hence, nothing other
than the following string should be displayed to the
console):
01111111, 127
- Then your code will return 0; and exit.
- You should then manually check that the portd.txt file
holds the string "01111011" (i.e. 123 in decimal)
as its first line in the file.
______ Read: "portb.txt" file instructions:
---------------------------------------------
F3.3 If there are only three valid input arguments passed to your
solution, and the first, second and third
input arguments are "-b", "io" and "portb", respectively: Your
solution must call the FileReader() function with
the first argument of the FileReader() function being set to
"portb.txt" and the second argument as a reference
to a suitably sized null terminated character array that can store
the characters to represent an 8-bit binary
number between 0 and 11111111 (under normal conditions).
Note: Under normal operation the string containing the 8-bit
binary value will be a string of eight characters
with each character either being a zero ('0') or a one ('1'). The
MSB (i.e. bit-7) will be the left-hand most
value and LSB being bit-0 will be the most right-hand value (e.g.
b7,b6,b5,b4,b3,b2,b1,b0). The same will apply
for portd.txt (F3.4) and pinc.txt (F3.5).
F3.3.1 Your solution should then check that the returned string
from the FileReader() only contains the numerical
characters '0' or '1' and that when converted to decimal is between
the values 0 and 255, inclusive. The string
should also be between one (1) and eight (8) characters long
followed by a null terminator to be valid. If the
string fails to comply with these specifications or a file error
occurred, your solution should treat the situation
as a MURPHY'S LAW error and nothing other than 'Z' should be
displayed to the console. Your solution must always
return 0; and exit.
F3.3.2 If the value returned by the FileReader() function is
valid, your solution should then display the string
returned by the FileReader() function (or its second argument
reference) as an 8-bit binary string followed by
a comma character and a space (i.e. ", "), which is then followed
by its (converted) decimal value. As an example,
if the portb.txt file contains the string "11111110", the exact
format of console display output should be:
11111110, 254
No other characters should be displayed to the console window.
F3.3.3 If no errors occurred, then your solution should return 0
and exit. Else, if an error occurred during
any part of the process, or the value returned by the FileReader()
contains anything other than a string that
represents something that can be converted to an 8-bit binary
value, nothing other than the error code
should be displayed to the console. Any error associated with the
file IO operation (i.e. the provided
FileReader() function) or the returned string containing the binary
value should be treated as a MURPHY'S LAW
error and nothing other than 'Z' should be displayed to the
console. Your solution must always return 0 and exit.
F3. For each of the commands discussed above which don't result in an error code situation, there are file IO operation/
-
- Posts: 43759
- Joined: Sat Aug 07, 2021 7:38 am