C++ C++;校验和读取不存在换行符

C++ C++;校验和读取不存在换行符,c++,checksum,C++,Checksum,我正在对文件执行一个非常基本的校验和,方法是将输入文件读入一个字符数组,然后迭代该数组,并将每个字符添加到校验和中。问题是,当我这样做时,我所有的校验和都太高了10(10是换行符的ascii十进制值) 当我知道我的文本中没有换行符时,如何将换行符插入到我的代码中?即使是单行文本文件也会添加换行符 #include <iostream> #include <fstream> int main () { int fileLength = 0; std::if

我正在对文件执行一个非常基本的校验和,方法是将输入文件读入一个字符数组,然后迭代该数组,并将每个字符添加到校验和中。问题是,当我这样做时,我所有的校验和都太高了10(10是换行符的ascii十进制值)

当我知道我的文本中没有换行符时,如何将换行符插入到我的代码中?即使是单行文本文件也会添加换行符

#include <iostream>
#include <fstream>

int main () {
    int fileLength = 0;
    std::ifstream inputFile;
    char charArray[10000];
    int checkSumValue = 0;

    // open file in binary
    inputFile.open("/Path/To/File", std::ios::binary);

    // get file length, then return to beginning of file
    inputFile.seekg(0, std::ios_base::end);
    fileLength = inputFile.tellg();
    inputFile.seekg(0, std::ios_base::beg);

    // read all data from file into char array
    inputFile.read(charArray, fileLength);

    // iterate over char array, adding ascii decimal value to checksum
    for (int num = 0; num <= fileLength; num++) {
        std::cout << "Checksum value before iteration " << num << " is " 
        << checkSumValue << std::endl;
        checkSumValue += static_cast<int>(charArray[num]);
    }

    // properly close out the input file
    inputFile.close();
    inputFile.clear(std::ios_base::goodbit);  

    std::cout << "The checksum value is: " << checkSumValue << std::endl;
    std::cout << "The file length is: " << fileLength << std::endl;

    return 0;
}
#包括
#包括
int main(){
int fileLength=0;
std::ifstream输入文件;
charchararray[10000];
int checkSumValue=0;
//以二进制文件打开文件
open(“/Path/To/File”,std::ios::binary);
//获取文件长度,然后返回到文件的开头
seekg(0,std::ios\u base::end);
fileLength=inputFile.tellg();
seekg(0,std::ios\u base::beg);
//将文件中的所有数据读入字符数组
读取(字符,文件长度);
//迭代字符数组,将ascii十进制值添加到校验和
对于(int num=0;num,您的问题是:

num <= fileLength
非常危险,因为
文件长度
可能大于数组的大小。
更好的解决方案是使用向量(因为它可以动态调整大小)

std::矢量字符(文件长度);
inputFile.read(&charArray[0],文件长度);
但是,您真的需要将数据复制到数组中吗?为什么不直接进行求和呢

size_t checkSumValue = std::accumulate(std::istreambuf_iterator<char>(fileLength),
                                       std::istreambuf_iterator<char>(),
                                       size_t(0)
                                      );
size\u t checkSumValue=std::accumulate(std::istreambuf\u迭代器(fileLength),
std::istreambuf_迭代器(),
尺寸(0)
);

Martin也是正确的-在所有情况下都应该是(num
另一种可能是你在编辑器中创建了文件,并且人为地为你添加了一个伪换行符。这很常见。尝试在十六进制编辑器中转储你的文件。我刚刚运行了你的程序(对于Ahh,但问题是:我在一个只有两个字符的文件上显示了3的长度:
At
。即使将for循环更改为
num
,我仍然可以看到添加了换行符。唯一删除它的方法是将我的代码更改为
num
。但是,我不知道是什么目前,我看到的是独立于平台的,所以我可能会在windows中添加此代码来破坏我的应用程序。@马丁:你也不应该将整个文件读取到内存中。如果打开10G数据文件,会发生什么情况?以小块读取该文件。@Moses:如果文件长度为3,则文件中有三个字符。(0、1、2)用另一个工具签出该文件以进行验证,但文件中有三个字符。@Martin我尝试过使用terminal
cat>>newfile
命令创建文本文件,我尝试过在vim
vim newfile
中创建文件,我尝试过在TextWrangler中创建文件。这三种方法都会将两个字符读作三个字符,即d计算一个错误的额外行…但由于您没有看到这一点,而且我怀疑vim和cat是否增加了任何开销,我将假设这是xcode的一个错误,因为我的代码为您工作。@Moses:这不是xcode。xcode只是一个使用普通编译器的环境。从命令行尝试
wc-c
这将告诉您您需要知道文件中有多少个字符。请尝试使用此选项创建一个包含2个字符的文件
echo-n AT>
inputFile.read(charArray, fileLength);
std::vector<char>   charArray(fileLength);
inputFile.read(&charArray[0], fileLength);
size_t checkSumValue = std::accumulate(std::istreambuf_iterator<char>(fileLength),
                                       std::istreambuf_iterator<char>(),
                                       size_t(0)
                                      );