Tip of the day
News
Instructions
Objective
Write a java homework program to generate checksums for the text in a file.
Requirements and Specifications
1 Checksum
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, or 32 bits). The program must generate output to the console (terminal) screen as specified below.
1.1 Command line parameters
- Your program must compile and run from the command line.
- Input the required file name and the checksum size 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 input text files, echo the processed input to the screen,make the necessary calculations, and then output the checksum to the console (terminal)screen in the format described below.
1.2 Checksum size
The checksum size is a single integer, passed as the second command line argument. The valid values are the size of the checksum, which can be either 8, 16, or 32 bits. Therefore, if the second 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:
fprintf(stderr, "Valid checksum sizes are 8, 16, or 32\n");
The message should be sent to STDERR1.
1.2.1 Format of the input file
The input file specified as the first command line argument, will consist of the valid 8 bit ASCII characters normally associated with the average text file. This includes punctuation, numbers, special characters, and whitespace.
Screenshots of output
Source Code
/*=============================================================================| Assignment: pa02 - Calculating an 8, 16, or 32 bit| checksum on an ASCII input file|| Author: Your name here| Language: c, c++, Java, GO, Python|| To Compile: javac pa02.java| gcc -o pa02 pa02.c| g++ -o pa02 pa02.cpp| go build pa02.go| python pa02.py //Caution - expecting input parameters|| To Execute: java -> java pa02 inputFile.txt 8| or c++ -> ./pa02 inputFile.txt 8| or c -> ./pa02 inputFile.txt 8| or go -> ./pa02 inputFile.txt 8| or python -> python pa02.py inputFile.txt 8| where inputFile.txt is an ASCII input file| and the number 8 could also be 16 or 32| which are the valid checksum sizes, all| other values are rejected with an error message| and program termination|| Note: All input files are simple 8 bit ASCII input||| Instructor:| Due Date: per assignment|+=============================================================================*/#include <stdio.h>#include <stdlib.h>/* * Print a file to stdout, prints only 80 characters per line * Returns the file size in bytes */int print_file(FILE *file){ unsigned int file_size = 0; int len = 0; char c; while (!feof(file)) { c = fgetc(file); if (!feof(file)) { printf("%c", c); if (c != '\n') { len++; if (len == 80) { printf("\n"); len = 0; } } else len = 0; file_size++; } } return file_size;}/* * Calculates the 8 bit checksum for a file, saves padding in passed argument */unsigned long int checksum8(FILE *file, int *padding){ unsigned long checksum = 0; unsigned char c; while (!feof(file)) { c = fgetc(file); if (!feof(file)) { checksum += c; checksum &= 0xFF; } } *padding = 0; /* no padding */ return checksum;}/* * Calculates the 16 bit checksum for a file, saves padding in passed argument */unsigned long int checksum16(FILE *file, int *padding){ unsigned long checksum = 0; unsigned char in[2]; unsigned int c; int i, n; *padding = 0; while (!feof(file)) { n = fread(in, 1, 2, file); /* read 2 bytes at a time */ if (n > 0) { /* add padding if read bytes < 2 */ for (i = n; i < 2; i++) { (*padding)++; in[i] = 'X'; } /* make int from the chars */ c = (in[0] << 8) | (in[1]); checksum += c; checksum &= 0xFFFF; } } return checksum;} /* * Calculates the 32 bit checksum for a file, saves padding in passed argument */unsigned long int checksum32(FILE *file, int *padding){ unsigned long checksum = 0; unsigned char in[4]; unsigned int c; int i, n; *padding = 0; while (!feof(file)) { n = fread(in, 1, 4, file); /* read 4 bytes at a time */ if (n > 0) { /* add padding if read bytes < 4 */ for (i = n; i < 4; i++) { (*padding)++; in[i] = 'X'; } /* make int from the chars */ c = (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | (in[3]); checksum += c; checksum &= 0xFFFFFFFF; } } return checksum;}int main(int argc, char **argv){ FILE *file; unsigned int size; unsigned int file_size; unsigned long int checksum; int i, padding; if (argc != 3) { fprintf(stderr, "Usage:\n %s input size", argv[0]); return 0; } size = atoi(argv[2]); /* convert second argument string to a number */ if (size != 8 && size != 16 && size != 32) { fprintf(stderr, "Valid checksum sizes are 8, 16, or 32\n"); return 1; } file = fopen(argv[1],"rt"); if (file == NULL) { fprintf(stderr, "Unable to open file %s\n", argv[1]); return 1; } /* print initial newline to match output format */ printf("\n"); /* print the file contents and get file size */ file_size = print_file(file); /* restore file pointer to start of file */ fseek(file, 0, SEEK_SET); /* calculate checksum */ switch(size) { case 8: checksum = checksum8(file, &padding); break; case 16: checksum = checksum16(file, &padding); break; case 32: checksum = checksum32(file, &padding); break; } fclose(file); /* print padding chars */ for (i = 0; i < padding; i++) printf("X"); printf("\n"); printf("%2d bit checksum is %8lx for all %4d chars\n", size, checksum, file_size + padding); return 0;}
Related Samples
Explore our C Assignments Sample Section, offering meticulously crafted solutions. From basic syntax to advanced algorithms, delve into concise, well-commented code examples. Perfect for students honing their C programming skills and mastering concepts like pointers, arrays, and dynamic memory allocation. Accelerate your learning and excel in your assignments effortlessly.
C
Word Count
24995 Words
Writer Name:Philip Dudley
Total Orders:2435
Satisfaction rate:
C
Word Count
4788 Words
Writer Name:James Trafford
Total Orders:567
Satisfaction rate:
C
Word Count
4564 Words
Writer Name:John Smith
Total Orders:590
Satisfaction rate:
C
Word Count
7928 Words
Writer Name:John Smith
Total Orders:590
Satisfaction rate:
C
Word Count
4428 Words
Writer Name:Emma Clarke
Total Orders:700
Satisfaction rate:
C
Word Count
4264 Words
Writer Name:Dr. Miriam Goldbridge
Total Orders:812
Satisfaction rate:
C
Word Count
6407 Words
Writer Name:Dr. Miriam Goldbridge
Total Orders:812
Satisfaction rate:
C
Word Count
7276 Words
Writer Name:Prof. Benjamin Mitchell
Total Orders:582
Satisfaction rate:
C
Word Count
5057 Words
Writer Name:Dr. Sophia Reynolds
Total Orders:458
Satisfaction rate:
C
Word Count
4612 Words
Writer Name:Dr. Jonathan Wright
Total Orders:812
Satisfaction rate:
C
Word Count
4457 Words
Writer Name:Dr. Nathan Nguyen
Total Orders:526
Satisfaction rate:
C
Word Count
3950 Words
Writer Name:Mark Davis
Total Orders:429
Satisfaction rate:
C
Word Count
3990 Words
Writer Name:Prof. Elizabeth Taylor
Total Orders:745
Satisfaction rate:
C
Word Count
3319 Words
Writer Name:Mark Davis
Total Orders:429
Satisfaction rate:
C
Word Count
3127 Words
Writer Name:Emma Clarke
Total Orders:700
Satisfaction rate:
C
Word Count
6063 Words
Writer Name:Dr. Jonathan Wright
Total Orders:812
Satisfaction rate:
C
Word Count
4724 Words
Writer Name:Dr. Jonathan Wright
Total Orders:812
Satisfaction rate:
C
Word Count
3095 Words
Writer Name:Dr. Deborah R. Craft
Total Orders:700
Satisfaction rate:
C
Word Count
5089 Words
Writer Name:Dr. Sophia Reynolds
Total Orders:458
Satisfaction rate:
C
Word Count
5780 Words
Writer Name:Emily Johnson
Total Orders:412
Satisfaction rate: