45.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 the name of the input file for calculating the checksum. The second parameter will be for the size of the checksum (8, 16 Command Line Parameters 1. 2. Your program must compile and run from the command line. The program executable must be named "checksum" (all lower case, no spaces or file extension) 3. nput 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 name of the file used for calculating the checksum, as described below. The second parameter must be the size, in bits, of the checksum. The sample run command near the end of this document contains an example of how the parameters will be entered Your program should open the two files, echo the processed input to the screen, make the necessary calculations, and then output the ciphertext to the console (terminal) screen in the format described below. 4. 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 elther 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: printf "Valid checksum sizes are 8, 16, or 32ln):; The message should be sent to STDERR. 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.

2 Answers

4 votes

Final answer:

To calculate the checksum for a text file, write a program that takes the input file name and checksum size as command line parameters. Open the input file, perform the calculations, and output the ciphertext. Handle invalid parameter values and ensure the input file contains valid ASCII characters.

Step-by-step explanation:

To calculate the checksum for a text file, you will need to write a program that takes two command line parameters. The first parameter should be the name of the input file, while the second parameter should be the size of the checksum in bits (8, 16, or 32). The program should open the input file, perform the necessary calculations, and output the ciphertext to the console screen.

If the first parameter is not one of the valid values (8, 16, or 32), the program should display an error message to the user on STDERR. The input file should contain valid ASCII characters associated with a text file, including punctuation, numbers, special characters, and whitespace.

User Nils Petersohn
by
7.6k points
0 votes

The C program below that calculates the checksum for the text in a file.

#include <stdio.h>

#include <stdlib.h>

#include <stdint.h>

// Function to calculate checksum based on the specified size

uint32_t calculateChecksum(FILE *file, int checksumSize) {

uint32_t checksum = 0;

while (!feof(file))

char ch = fgetc(file);

checksum = (checksum << 8)

// Truncate the checksum based on the specified size

checksum = checksum & ((1 << checksumSize) - 1);

return checksum;

}

int main(int argc, char *argv[]) {

// Check the number of command line arguments

if (argc != 3) {

fprintf(stderr, "Usage: %s <input_file> <checksum_size>\\", argv[0]);

return EXIT_FAILURE;

}

// Parse the checksum size

int checksumSize = atoi(argv[2]);

// Check if the checksum size is valid

if (checksumSize != 8 && checksumSize != 16 && checksumSize != 32) {

fprintf(stderr, "Valid checksum sizes are 8, 16, or 32.\\");

return EXIT_FAILURE;

}

// Open the input file

FILE *inputFile = fopen(argv[1], "rb");

if (inputFile == NULL) {

perror("Error opening input file");

return EXIT_FAILURE;

}

// Display the processed input

printf("Processed Input:\\");

fseek(inputFile, 0, SEEK_SET);

int ch;

while ((ch = fgetc(inputFile)) != EOF) {

putchar(ch);

}

printf("\\");

// Calculate and display the checksum

uint32_t checksum = calculateChecksum(inputFile, checksumSize);

printf("Checksum (%d bits): %u\\", checksumSize, checksum);

// Close the input file

fclose(inputFile);

return EXIT_SUCCESS;

}

To Compile the program using a C compiler (e.g., gcc): gcc -o checksum checksum.c

So, to run the program using the sample command: ./checksum input.txt 8

User Zuboje
by
8.2k points