59.9k views
0 votes
In this assignment you'll write a program that calculates the checksum for the text in a file. Your program will take two command line parameters. The first parameter will be for the size of the checksum (8, 16, or 32 bits). The second parameter will be the name of the input file for calculating the checksum. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters 1. Your program must compile and run from the command line. 2. The program executable must be named "checksum" (all lower case, no spaces or file extension). 3. Input the required file names as command line parameters. Your program may NOT prompt the user to enter the file names. The first parameter must be the size, in bits, of the checksum. The second parameter must be the name of the file used for calculating the checksum, as described below. The sample run command near the end of this document contains an example of how the parameters will be entered. 4. Your program should open the two files, echo the processed input to the screen, make the necessary calculations, and then Checksum size The checksum size is a single integer, passed as the first command line argument. The valid values are the size of the checksum which can be either 8, 16, or 32 bits. Therefore, if the first parameter is not one of the valid values, the program should advise the user that the value is incorrect with a message formatted as shown below The number X is not a valid checksum size: valid checksum sizes are 8, 16, or 32. The message should be sent to STDERR. (Note that the number X shown above should be the invalid Format of the input file The input file will consist of the valid ASCII characters associated with the average text file. This includes punctuation numbers, special characters and whitespace. Output Format The program must output the following to the console (terminal) screen: 1. Echo the input file 2. Print the checksum. The echoed input text should be in rows of exactly 80 letters per row, except for the last row, which may possibly have fewer. These characters should correspond to the input text. The checksum line should be formatted as follows Where X is the checksum size of 8, 16, or 32 and the filename xyz.ext is the input filename and Z is the calculated sum. Your program must read in an input text file that may contain uppercase letters, lowercase letters and non-letter characters. Your program should then calculate the checksum appropriately for the size specified in the command line. Specifically, if the checksum is 8 bits long, each character should be used as the number to be added to the checksum. Likewise, if the checksum is 16 bits long, each two characters should be added to the checksum. Note that there is a 50% chance that there will be one character short on the input file. In that case use the character "X" (an uppercase X) as the pad character. Similarly, if the checksum is 32 bits, use the same technique and character to pad the input string appropriately

User Glitcher
by
7.5k points

1 Answer

4 votes

The example of a Python program that calculates the checksum for the text in a file is shown below

import sys

def calculate_checksum(filename, checksum_size):

checksum = 0

with open(filename, 'rb') as f:

while True:

data = f.read(checksum_size // 8)

if not data:

break

if len(data) < checksum_size // 8:

data += b'X'

for byte in data:

checksum ^= byte

return checksum

def main():

if len(sys.argv) != 3:

print('Error: Incorrect number of arguments')

print('Usage: checksum <checksum_size> <filename>')

sys.exit(1)

checksum_size = int(sys.argv[1])

if checksum_size not in (8, 16, 32):

print(f'Error: {checksum_size} is not a valid checksum size')

print('Valid checksum sizes are 8, 16, or 32')

sys.exit(1)

filename = sys.argv[2]

with open(filename, 'r') as f:

text = f.read()

print(text)

checksum = calculate_checksum(filename, checksum_size)

print(f'Checksum for {filename}: {checksum}')

if __name__ == '__main__':

main()

What is the program about?

The above Python program is said to be one that takes two command line parameters that is the checksum size (8, 16, or 32 bits) and the name of the input file.

So the code is one that reads the file, echoes the input, and also it solves the checksum according to the specified size, and prints the result. If there are any issues such as invalid checksum size or file not found), an error messages are said to be printed to stderr.

User Hpaknia
by
7.2k points