First off, I am completely new to C++. Currently I am in an online class (CS 161) and the teacher isn't very responsive. I'm not completely understanding everything 100% and am looking for some help/explanations. First I'll post the code that I have so far and then what the assignment is looking for to give you background. Here are the questions I have;
1. The assignment is telling me to use inFile.read(charArr, fileLen); to read the entire file into my char array, but I keep getting declaration errors no matter what I use (int, string, ifstream, etc). Am I missing something? Its currently not in my code at the moment due to the issues.
2. I am looking to make it so after it spits out the checksum, it returns to allow the user to the top of the selection screen (unless the selection was Q of course). I *think* I should use two functions for this, but anytime I try to separate the input/output and the opening of the file into separate functions, again errors.
3. Finally, when I do get a checksum its hexidecimal I think. Instead of a number it is spitting out 0038FCB4 on a text file given (can provide if needed). According to the example, it should just be a number.
Here is my code (its very basic, again I'm still learning and I am not looking for anyone to do my homework for me, just someone to point me in the correct direction..)
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// Declare Variables
char reply;
string filePath;
int main()
{
const int SUM_ARR_SZ = 100;
unsigned int checkSums[SUM_ARR_SZ];
string fileNames[SUM_ARR_SZ];
ifstream inFile;
cout << "Please select: " << endl;
cout << "A) Compute checksum of specified file " << endl;
cout << "B) Verify integrity of specified file " << endl;
cout << "Q) Quit " << endl;
cin >> reply;
switch (reply)
{
case'A':
case'a':
cout << "Specify the file path: " << endl;
cin >> filePath;
break;
case'B':
case'b':
cout << "Specify the file path: " << endl;
cin >> filePath;
break;
case'Q':
case'q':
break;
default:
cout << "Invalid Input." << endl;
}
inFile.open(filePath.c_str(), ios::binary);
inFile.seekg(0, ios_base::end);
int fileLen = inFile.tellg();
inFile.seekg(0, ios_base::beg);
cout << "File checksum = " << checkSums << endl;
inFile.close();
inFile.clear(std::ios_base::goodbit);
system("pause");
return 0;
}
|
Here is the assignment. Thank you.
Your assignment is to write a C++ program which will automatically compute the checksum on a specified data file, and then store that checksum in an unsigned integer array. The program must also store the file name in another, parallel array. You can use these declarations in your program:
const int SUM_ARR_SZ = 100;
unsigned int checkSums[SUM_ARR_SZ];
string fileNames[SUM_ARR_SZ];
This gives your program the flexibility to run checksums on several files (up to 100) and remember which sums go with which files.
Your program should then be able to verify that a specified file has not been compromised by running the checksum over it and verifying that the checksum is what it was previously.
Your program *must* read the file into a char array. Make the array size 100000 chars so that it is large enough for our file. After the file is stored in the array, your program must loop through the array to compute the checksum using the algorithm described above (simple sum).
Since this is essentially an operation on a binary file (even though text may be stored in the file), open the file in binary mode to prevent the system from performing any interpretation on the data like this:
inFile.open(filePath.c_str(), ios::binary);
To get the file size, use the seekg and tellg operators like this:
inFile.seekg(0, ios_base::end);
int fileLen = inFile.tellg();
inFile.seekg(0, ios_base::beg);
The first line moves the file cursor to the end of the file. The second line reads the byte position at that current location. The third statement moves the file cursor back to the beginning of the file so the data can be read from the beginning.
After that, you can read the entire file contents into your char array in one statement like this:
inFile.read(charArr, fileLen);
Then the file must be closed:
inFile.close();
The following line seems to be required to put the file back to a state where it can be opened again it (otherwise, some compilers, like mine, will not allow the file to be reopened):
inFile.clear(std::ios_base::goodbit);
For comparing file names, use the strcmp() built in function.
High level pseudocode for determining the checksum may look something like this:
1. Open the specified file in binary mode
2. Save the file name in the fileNames array.
3. Determine the file size using seekg and tellg
4. Read the file contents into the character array in one statement
5. Close the file
6. Loop through the array, one character at a time and accumulate the sum of each byte
7. Store the sum into the checkSums array.
Sample Dialog:
Note: In the example below, the user’s input is in RED BOLD. The program output is not. Also, your code should allow menu entry input in a case insensitive manner.
Please select:
A) Compute checksum of specified file
B) Verify integrity of specified file
Q) Quit
a
Specify the file path: c:\temp\tmp1
File checksum = 1530
Please select:
A) Compute checksum of specified file
B) Verify integrity of specified file
Q) Quit
a
Specify the file path: c:\temp\tmp2
File checksum = 29430
Please select:
A) Compute checksum of specified file
B) Verify integrity of specified file
Q) Quit
b
Specify the file path: c:\temp\tmp1
File checksum = 1530
The file integrity check has passed successfully (checksum = 0).
Please select:
A) Compute checksum of specified file
B) Verify integrity of specified file
Q) Quit
b
Specify the file path: c:\temp\tmp1
File checksum = 1597
The file integrity check has failed (previous checksum = 1530, new checksum = 1597)
Please select:
A) Compute checksum of specified file
B) Verify integrity of specified file
Q) Quit
b
Specify the file path: c:\temp\tmp3
File checksum = 5596
The checksum has not been computed on the file: c:\temp\tmp3
Please select:
A) Compute checksum of specified file
B) Verify integrity of specified file
Q) Quit