Random extra characters in string to char conversion

Anybody know why the conversion of string "CT/C1.dcm" to char[string.size()] is going to "CT/C1.dcmx?2?" ?


Here is my output:

Reading File: CT/C1.dcm of type CT
You are here
you are here with 9
you are here with CT/C1.dcmx?2?
Unsuccessful open
now here
here
Segmentation fault: 11


and the code is:
filename += ".dcm";
G4cout << "Reading File: " << filename << " of type " << fileType << G4endl;
G4cout << "You are here" << G4endl;
char * name = new char[filename.size()];
G4cout << "you are here with " << filename.size() << G4endl;
for(unsigned short i=0;i<filename.size();i++) { name[i]=filename[i]; }
FILE * dicom;
G4cout << "you are here with " << name << " " << G4endl;
dicom = std::fopen(name,"rb");
if(dicom!=NULL) G4cout << "Successful open" << G4endl;
else G4cout << "Unsuccessful open" << G4endl;
char * buffer = new char[LINEBUFFSIZE];
G4cout << "now here" << G4endl;
implicitEndian = false;
littleEndian = true;
G4cout << "here" << G4endl;
std::fread( buffer, 1, 128, dicom);
std::fread( buffer, 1, 4, dicom);
G4cout << "here and here" << G4endl;
if(std::strncmp("DICM",buffer,4) != 0) {
std::fseek(dicom, 0, SEEK_SET);
implicitEndian = true;
}
G4cout << "and here" << G4endl;
I'm having trouble understanding what you are trying to accomplish with this code. However is you have a string variable "s," you could convert it to char[] using:
s.c_str();

I hope this helps.

If not, try simplifying your code to a very simple case or explain your code further (comments help me).
That definitely helped. Thank you.
Topic archived. No new replies allowed.