What I want to do is to convert a string into const char*.
Ideally I could just use "const char* myConstChar = myString.c_str()" but as my example below shows; it doesn´t work well for binary data:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
string myString; // This need to be string
ifstream infile;
const char* myConstChar; // This need to be const char*
infile.open("binary.bin");
if(infile){
while(getline(infile, myString)){
std::cout << "Data: " << myString << " string length: ";
std::cout << myString.length() << "\n";
myConstChar = myString.c_str();
std::cout << "const char Data: " << myConstChar;
std::cout << " const char length: "<< strlen(myConstChar) <<"\n";
}
}
infile.close();
return 0;
}
This returns "string length: 13" and "const char length: 3".
Apparently there are some loss of data when converting string to const char* using myString.c_str()!
How do I convert a string to const char* without losing binary data?!
std::vector<char>.