CRC program

Here is my assignment:

CIS 215 C++ Programming
Programming Assignment #2
Due Date: 10/21/13
The Cyclic Redundancy Check is an algorithm that is used to find errors in data that has been stored or transmitted. Since every file or message can be thought of as a single pattern of bits it can be treated as a single large binary number. The CRC technique makes use of a polynomial divisor of length n+1 to divide the source number – discarding the quotient and using the remainder as the check sum for that message.
The result of the CRC algorithm is a check value that can be used to determine whether or not the file or message being examined is the same file or message that was used to create the check value.
References:
For more background information on CRCs I recommend the “Painless Guide to CRC Error Detection Algorithms” by Ross Williams, which you should be able to find at http://www.ross.net/crc/download/crc_v3.txt. Note that this guide provides C language source code for 16 and 32 bit CRC implementations. You may look at that code for ideas, but you may not copy it verbatim into your assignment.
The concept of CRC error checks is easy – but it is often tricky to get the correct answer. Padding the message with zeros, and mismatches between the size of your divisor, and the fields you are considering are a frequent source of errors. Also, keep in mind that the divisor is one bit larger than the width of the field it is correcting – so a CRC-5 polynomial actually has 6 bits.
Specification:
There are many polynomial divisors of different lengths used to calculate CRCs. For this assignment you should use the CRC-5-USB value (0x12).
Implement a checksum program for CRC-5-USB that accepts the name of a file from the command line and prints your signature block and the correct checksum on console standard output.
Implement a checksum program for CRC-5-USB that accepts input from console standard input and writes your signature block and the correct checksum on console standard output.
Implement a checksum program for CRC-5-USB that accepts the name of a file from the command line and prints your signature block and PASS or FAIL for a file with an appended checksum.
Run your program against testfile.txt to produce a checksum.
Run your program against the input “Four score and seven years ago” (not including the quotation marks) to produce a checksum. Confirm that the checksum you receive is 0x8.
Run your program against testfile_appended.txt to produce a PASS/FAIL result.
Run your program against testfile_appended_bad.txt to produce a PASS/FAIL result.
The checksum program should be a single program with an interface that allows the user to choose which mode it is running in (File, text, and print checksum or pass/fail).


My problem is that I have ZERO experience in programming (except for a hello world program) Everything I have read seems to be written in a foreign language. I have created a dummy user interface, but that is as far as I have been able to get. If there is someone out there that can explain this assignment to me is SIMPLE terms, that would be great!

1. How would I upload a file into the program? I have tried a few things, but it doesn't seem to work.

2. Now that I have uploaded the file, how would I convert the info in the file to binary?

3. Polynomial division: I understand what it does, but not sure how to write the code.

Below is my 'dummy' interface:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <fstream>
#define SignatureBlock "~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\nNicholas Whitmore\nnicholas.whitmore@maine.edu\nCIS 215, C++ Application Programming\n~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\n"

using namespace std;
using std::string;


int main()
{
    cout << SignatureBlock << endl;//implement signature block
    
    cout << "" << endl;
    cout << "CRC Check Program\n\n" << endl;//title
    
    cout << "Would you like to check a file or a string?\n" << endl; //prompt for user input
    cout << "1) To Check a File\n" << endl;//option 1
    cout << "2) To Check a String\n" << endl;//option 2
    cout << "3) To exit\n" << endl;//option 3
    
    int select; //user selection variable
    cin >> select;//user input
        if (select == 1)//user chose option 1
            {
                cout << "Enter the name of the file including path:" << endl;//prompt for file path
	            string filePath; //file path variable
                cin.ignore();
	            getline(cin, filePath); //user input
	            cout<<"You have entered \"" << filePath << "\"!"<< endl;
                
            }
        else if (select == 2)//user chose option 2
            {
                cout << "Enter the string to be checksummed:" << endl;//prompt for string
                string cString; //string to be checked variable
                cin.ignore();
                getline(cin, cString);//user input
                cout<<"You have entered \"" << cString << "\"!"<< endl;
            }
        else if (select == 3)//user chose option 3
            {
            cout << "Thank you! Have a nice day!" << endl; //exits the program
            }
        else//user chose an invalid option
            {
            cout << "You have chosen an invalid option. Good bye." << endl;//invalid option. program closes
            }
    cin.get(); //keeps the command prompt window open
    return 0;
}//end main()
Last edited on
You don't 'upload' a file in C++ but you output or input it. But in either case, you open it. In your case if the user wants to input a file in binary mode(note a binary file must already exist in your project directory), it is done as follows:
1
2
ifstream FileIn;
FileIn.open("File_name.extension", ios::bin);


or directly

 
ifstream FileIn("File_name.extension", ios::bin);


Here, ios specifies the file open mode, which in this case in binary.

Hope that helped!
Last edited on
When I said 'upload' i meant open, LOL

I know, I'm a n00b *sigh*

I was able to open the file that I needed (and print the contents of the file on the screen). Now I am able to open the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 if (select == 1)//user chose option 1
            {
                cout << "Enter the name of the file including path:" << endl;//prompt for file path
	            string filePath; //file path variable
                cin.ignore();
	            getline(cin, filePath); //user input
                ifstream myfile(filePath.c_str());
                string line;
                if (myfile.is_open())
                {
                   while (getline(myfile,line))
                   {
                         cout << line << endl;
                   }
                   myfile.close();
                }
                else cout << "Unable to open file" << endl;
            }


Now, I am lost :( I apparently need to convert the file's contents into binary and do polynomial division. not sure where to start on this one. One problem solved now though, and 3 more days to write the code.
Last edited on
I have 2 text files that the professor wants us to use as the test files, named
testfile_appended.txt

and
testfile_bad_appended.txt


These files are what I will be checking my CRC algorithm against, as well as a user defined string:
Four score and seven years


this is where it gets really tricky for me. If I have the time, I will change my IF statement to a loop (that doesn't create an infinite loop, hopefully). Thanks for your input mirage!
Last edited on
Topic archived. No new replies allowed.