Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 卡在阵列上_C++_Arrays - Fatal编程技术网

C++ 卡在阵列上

C++ 卡在阵列上,c++,arrays,C++,Arrays,我是一名学生,不知道如何完成这项作业。基本上,我们应该自动计算数据文件上的校验和,并将该校验和存储在无符号整数数组中。文件名应该存储在另一个并行数组中,文件内容将被读入char数组以计算校验和 这就是我到目前为止所做的: #include <iostream> #include <string> #include <iomanip> #include <fstream> #include <cstring> using namespa

我是一名学生,不知道如何完成这项作业。基本上,我们应该自动计算数据文件上的校验和,并将该校验和存储在无符号整数数组中。文件名应该存储在另一个并行数组中,文件内容将被读入char数组以计算校验和

这就是我到目前为止所做的:

#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");
}
更新:我已经把程序的第一部分整理好了,就是检查总和的部分。我现在遇到的问题是,如果在菜单上选择B,输出是否正确。
它应该检查两个数组,确保名称正确,并确保校验和相同,但我完全不知道如何将其转换为代码。

将代码更改为从文件逐字符读取。 文件长度未知。 执行此操作直到文件结束:

inFile.seekg(0, ios_base::end);
int fileLen = inFile.tellg();
inFile.seekg(0, ios_base::beg);
for(int i =0; i<fileLen; i++)
{
    char Arr[1];
    inFile.read(Arr, 1);
    sum += Arr[0];    
}
inFile.close();
infle.seekg(0,ios_base::end);
int fileLen=infle.tellg();
填充seekg(0,ios_基::beg);

对于(int i=0;i问题在于
for
循环的限制。无论文件的长度如何,它都要读取
100000
次。将
100000
更改为
fileLen
限制读取到
Arr
中的每个字符:

for (i = 0; i < fileLen; i++) {
    sum += Arr[i];
}

您的代码假定文件长度始终相同。有两条一般规则比任何特定解决方案都更有价值:1)单独开发新功能,2)如果您在解决问题时遇到困难,请尝试解决更简单的问题。在本例中,您有一个字符串数组,并且希望在其中搜索给定的字符串。因此,请编写一些可以做到这一点的代码,不要将其集成到代码的其余部分中,直到它能够完美地工作。我认为这是错误的<代码>Arr
已经用文件内容填充。@David C.兰金:这不是问题。你的意思是什么?虽然这不是一个问题,但它也不能完成任何使用
infle.read(Arr,fileLen)尚未完成的工作。您可以自由地将文件重新读入
Arr
,该文件在
for
循环之前已读入
Arr
1行,但它只是重复的代码和对
Arr
的重新定义。你没有看到
infle.read(Arr,fileLen)?你的回答是对的。我以前在
时使用了
,并将其更改为
。我仍然得到一个很大的负数作为输出,我不知道为什么。由于输入文件只有几个字母,所以发生了一些有趣的事情。在开始读取之前,您需要确保确实成功地打开了文件。检查
是否(填充打开)
并将您的读取设置为成功打开。我猜你可能是在读坏消息。
for (i = 0; i < fileLen; i++) {
    sum += Arr[i];
}
$ ./bin/cscpp
Please select:
   A) Compute checksum of specified file
   B) Verify integrity of specified file
   Q) Quit
a
Specify the file path:
tmpkernel315.txt

    File checksum = 46173