C++ 校验和,数据完整性
此分配的伪代码实际上是: 1.以二进制模式打开指定的文件 2.将文件名保存在文件名数组中。 3.使用seekg和tellg确定文件大小 4.在一条语句中将文件内容读入字符数组 5.关闭文件 6.循环遍历数组,每次循环一个字符,并累加每个字节的总和 7.将总和存储到校验和数组中C++ 校验和,数据完整性,c++,arrays,data-integrity,C++,Arrays,Data Integrity,此分配的伪代码实际上是: 1.以二进制模式打开指定的文件 2.将文件名保存在文件名数组中。 3.使用seekg和tellg确定文件大小 4.在一条语句中将文件内容读入字符数组 5.关闭文件 6.循环遍历数组,每次循环一个字符,并累加每个字节的总和 7.将总和存储到校验和数组中 #include <iostream> #include <string> #include <iomanip> #include <fstream> #include &
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
//declare variables
string filePath;
void savefile();
char choice;
int i, a, b, sum;
sum = 0;
a = 0;
b = 0;
ifstream inFile;
//arrays
const int SUM_ARR_SZ = 100;
string fileNames[SUM_ARR_SZ];
unsigned int checkSums[SUM_ARR_SZ];
do {
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 >> choice;
if (choice == 'a' || choice == 'A')
{
//open file in binary mode
cout << "Specify the file path: " << endl;
cin >> filePath;
inFile.open(filePath.c_str(), ios::binary);
//save file name
fileNames[a] = filePath;
a++;
//use seekg and tellg to determine file size
char Arr[100000];
inFile.seekg(0, ios_base::end);
int fileLen = inFile.tellg();
inFile.seekg(0, ios_base::beg);
inFile.read(Arr, fileLen);
inFile.close();
for (i = 0; i < 100000; i++)
{
sum += Arr[i];
}
//store the sum into checkSums array
checkSums[b] = sum;
b++;
cout << " File checksum = " << sum << endl;
}
if (choice == 'b' || choice == 'B')
{
cout << "Specify the file path: " << endl;
cin >> filePath;
if (strcmp(filePath.c_str(), fileNames[a].c_str()) == 0)
{
}
}
} while (choice != 'q' && choice != 'Q');
system("pause");
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
//声明变量
字符串文件路径;
void savefile();
字符选择;
整数i,a,b,和;
总和=0;
a=0;
b=0;
河流充填;
//阵列
const int SUM_ARR_SZ=100;
字符串文件名[SUM_ARR_SZ];
无符号整数校验和[SUM_ARR_SZ];
做{
库特
您正在堆栈上创建一个数组,而没有将其内容归零,因此Arr
将包含“垃圾”数据
您正在创建具有固定大小的缓冲区,这意味着如果文件小于100000字节,并且无法处理大于100000字节的文件(如果不重用缓冲区),则会浪费空间
如果文件小于100000字节,则迭代缓冲区中的每个字节,而不是那些表示文件的字节
<> Li >我还注意到你正在混合C和C++字符串函数。你不需要调用C的<代码> STRCMP <代码> >如果你使用<代码>字符串< /代码>,则使用<代码>字符串::比较
C++不需要对局部变量进行前向声明,如果只在使用局部变量时声明它们,而不是一次声明所有变量,那么代码会更干净
谢谢你的帮助。我对C++编程很陌生,我不能让老师给我发电子邮件求助。