c++ – How can I change "\ n" to "\ r \ n" in a char in c ++


When reading a file with the following code:

std::ifstream t;
int length;
t.open(fileName_char);      // open input file
t.seekg(0, std::ios::end);    // go to the end
length = t.tellg();           // report location (this is the length)
t.seekg(0, std::ios::beg);    // go back to the beginning
char *buffer = new char[length];    // allocate memory for a *buffer of appropriate dimension
t.read(buffer, length);       // read the whole file into the buffer

Line breaks are stored in the buffer as "\ n". (Ex: "Hello \ nWorld"). Well, the problem comes when I want to get the HASH SHA1 code with the following code:

TIdHashSHA1* sha1CODE = new TIdHashSHA1;
GeneratedHASHcode = sha1CODE->HashStringAsHex(buffer);
delete sha1CODE;

The sha1CODE-> HashStringAsHex () function does not encode "\ n" in the same way as "\ r \ n", so it generates a HASH SHA1 code different from the one I have saved in the database. Finally, when I compare the 'Generated HASHcode' with the 'Database_HASHcode' it obviously indicates that there is no match and it gives me an error.

How can I change "\ n" to "\ r \ n" in the buffer variable?

Thanks in advance


According to this answer . When you open a file in text mode, by default \ r \ n is converted to \ n, to avoid this, open the file in binary mode, which according to the documentation would be as follows:

t.open(fileName_char, ios::binary);
Scroll to Top