Char Array contains null characters before the end <p>I've got a class project to make a webserver in c++. Everything's been going OK until I got to the point where I needed to host images or pdfs, at which point the files were corrupted. Doing some more digging, I realized that all the corrupted images had null characters before the end. </p> <p>That brings me to my question. I have a char* which I've read these files to, and I know the length of the file. I'm pretty positive that the entire file is being read in (code below), but I don't know how to print it out or send it. How can I tell C++ that I want to send the first X characters following the char*? (I'm sure the answer is somewhere here or on the web, I just can't seem to phrase my question in the right way to find the answer)</p> <pre><code>ifstream myfile (path.c_str() , ios::in|ios::binary|ios::ate); ifstream::pos_type size = myfile.tellg(); cout &lt;&lt; size &lt;&lt; endl; fileSize = (int) size; fileToReturn = new char [size]; myfile.seekg (0, ios::beg); myfile.read (fileToReturn, size); myfile.close(); cout &lt;&lt; "file read\n"&lt;&lt; fileToReturn &lt;&lt; endl; </code></pre> <p>For a plain text file, this would output fine. For the PDF, it only prints the first part of the file (the part before the first null character). How do I get it to print out the whole file?</p> <p>EDIT: To clarify, my end goal is to send this over the web, not re-save the file.</p> <pre><code>// reply is string with all my headers and everything set. // fileToReturn is my char*, and fileSize is the int with how long it should be char* totalReply = new char [reply.length() + fileSize+1]; strcpy(totalReply, reply.c_str()); strcat(totalReply, fileToReturn); send(client, totalReply, reply.length() + fileSize, 0); </code></pre>