0% found this document useful (0 votes)
19 views3 pages

Programming Assignment 1

The programming assignment asks students to write a C program that compares two files and reports the first difference found between the files. The program takes user input for the file names and whether to do case-sensitive or non-letter comparisons. It then loops through the files character by character, checking for mismatches and outputting the mismatch type or that the files match if no mismatch is found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Programming Assignment 1

The programming assignment asks students to write a C program that compares two files and reports the first difference found between the files. The program takes user input for the file names and whether to do case-sensitive or non-letter comparisons. It then loops through the files character by character, checking for mismatches and outputting the mismatch type or that the files match if no mismatch is found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSC 362 Programming Assignment #1

Due date: Friday, September 8

In this assignment, you will write a C program that deals with file I/O, printf and scanf statements,
and logic. Do not use arrays (except for storing strings for your filenames). Your program should
only consist of appropriate #include statements and a main function (do not use other functions).

The Linux cmp program compares two files and reports on the first mismatch found (if any). You
will write a similar program that operates as follows. Ask the user for the two filenames, whether
case sensitivity is desired and whether non-letters will be compared. The program will contain a
loop to iterate through each pair of characters (one from each file) to locate the first mismatch
between pairs of characters, or if there was no mismatch. The program will output the location and
type of mismatch (types are described below) or that there was no mismatch and if this is the case,
the size of the files. The mismatches are as follows with the error code number (discussed below):
• two of the same letter but wrong case (only if case sensitivity is asked for) (1)
• two different letters (2)
• a letter and a non-letter (3)
• two different non-letters (only if non-letters are compared) (4)
• first file is longer than second file (5) or second file is longer than first file (6)
• no mismatch means error code is 0

For instance, if one file stores foobar and the other fooBar and we ask for case sensitivity, we will
be told that the mismatch occurred at byte 4 and the mismatch was a b in the first file and a B in
the second file. If we did not ask for case sensitivity, the files would match and be 6 bytes long.
(Note: the size of the file is equal to the number of characters input, or the location of a mismatch
is the number of characters input that led to the mismatch). If the two files store foo:bar and foo-
bar and we ask for non-letter matches, we would get a mismatch at byte 4 of : and -, but if we do
not ask for non-letter matches, we would get no mismatches. If the two files are foobar and fo0bar,
there is a mismatch occurred at byte 3 between a letter (o) and a non-letter (0). Notice that this
mismatch is detected even if the user does not care about non-letter mismatches since one was a
letter.

The program will be implemented as follows.


• #include <stdio.h> and #include <ctype.h> (this second library contains
functions isalpha, toupper and tolower)
• Declare two string variables for the filenames (e.g., char varname[30]; assume
filenames won’t be more than 30 characters), four character variables (two to input the user
responses for case sensitive and non-letter matches, two to hold the current inputs, one
from each file), two FILE * variables to be used as the two file pointers, and two int
variables for the current location/size and the error code number which is initialized to 0
as we will assume no mismatches/errors unless we find one. You can add other variables
as or if you see fit.
• Input from the user the two file names. Open the files as read (“r”) files.
• Input from the user whether they want case sensitivity and whether they want to match
non-letters. To input a string, use scanf(“%s”, varname); and to input a char use
scanf(“ %c”, &varname); Notice the space before the %.
The examples we covered in class used a while loop that input the next char and tested it for EOF.
As we are inputting from two files, this doesn’t work. So instead, prior to your while loop use getc
to input the first char from each file. The while loop condition will be something like
while(char1!=EOF&&char2!=EOF&&error==0) where char1 and char2 are the two
characters and error is the current error code (initialized to 0). You are testing the error code
here because if you find an error you should leave the loop instead of continuing to work through
the files. Note that you should use variable names that you like rather than char1, char2, error.

Inside the while loop, increment the counter/location/number of bytes int value, and then use logic
test to test for possible errors. Inside the loop, only test for errors 1-4 (testing for errors 5-6 will be
handled after the loop). It is suggested that you use nested if-else logic. Note that char1!=char2
does not necessarily mean a mismatch as the user may not care about case sensitivity or non-letter
mismatches. So you will have to express the errors using more complex logic. At the bottom of
the loop, if no error was detected in this iteration, input the next pair of chars from the files (do not
input if an error was detected because this will throw off the output).

After the loop, if there has been no error then the reason you left the loop was because at least one
file had ended and so test to see if the other file has ended. If char1!=EOF then file2 ended but
file1 did not so file1 is longer. If char2!=EOF then file1 ended but file2 did not so file2 is longer.
Next, output the result. Use a switch or nested if-else to test the error code to decide what to output.
If there was no mismatch, output that there was no mismatch and then the size of the two files. If
there was a mismatch, output the character found in each file at the point of mismatch and the
location of the mismatch. If one file ended and the other did not, output which file ended, which
did not, and how long the ended (shorter) file is. Finally, close both files. Example output is shown
on the next page of this assignment.

There are eight pairs of files on Canvas to go along with this pdf. Download them. Write and debug
your program and then run your program on the first three pairs of files and compare your output
to the output given on the next page of this assignment to determine if you have logic errors. Fix
any errors you find. When your program is running correctly, run your program on the last five
pairs of files, collect the outputs from these five runs and paste the outputs at the bottom of your
C file in comments. Submit the single .c file by email to [email protected]. Do not attempt to upload
your assignment to Canvas and do not submit the project file, only the .c (or .cpp) file.

Remember to comment your code well (see the example on Canvas for commenting expectations).
Make sure your name appears in the comments at the top of the program.

If you have difficulties with the syntax of C, with your C compiler, with the logic of the program,
or need help debugging any aspect of your program, email [email protected] for help. When emailing,
make sure you attach the .c file to the email and in the body, describe the issue(s) you are having.
The following are sample inputs and outputs for the first 3 pairs of datafiles. Your outputs
(including your prompting statements) do not need to be the same, but you must correctly identify
the type of mismatch (if any) and the location or file size.

Run 1:
Input:
Input filename1: p1in1a.txt
Input filename2: p1in1b.txt
Case sensitive? (Y/N): Y
Match non-letters? (Y/N): N

Output:
Mismatched case at 4 with p1in1a.txt having b and p1in1b.txt having B

Run 2:
Input:
Input filename1: p1in2a.txt
Input filename2: p1in2b.txt
Case sensitive? (Y/N): Y
Match non-letters? (Y/N): Y

Output:
Files are the same with length of 6

Run 3:
Input:
Input filename1: p1in3a.txt
Input filename2: p1in3b.txt
Case sensitive? (Y/N): N
Match non-letters? (Y/N): Y

Output:
Mismatched letter/non-letter at 2 with p1in3a.txt having 0 and p1in3b.txt
having o

Run your program on files 4-8 with the following inputs for case sensitive and match non-letters
respectively:

Files p1in4a.txt/p1in4b.txt: N, N
Files p1in5a.txt/p1in5b.txt: Y, N
Files p1in6a.txt/p1in6b.txt: N, N
Files p1in7a.txt/p1in7b.txt: N, N
Files p1in8a.txt/p1in8b.txt: N, Y

You might also like