×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Generate Checksums For File In C Or Java Language Assignment Solution

July 03, 2024
John Smith
John Smith
🇦🇺 Australia
C
With a master's degree in computer science and over 800 completed orders, John specializes in algorithm design and optimization for Mapping module assignments in C programming. His in-depth knowledge of data structures and efficient coding practices ensures top-notch solutions for every assignment.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

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
  1. Your program must compile and run from the command line.
  2. 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.
  3. 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
program to generate checksums for file in C or Java
program to generate checksums for file in C or Java 1
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.