Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:44:27+00:00 2026-06-13T09:44:27+00:00

I’ve got a class project to make a webserver in c++. Everything’s been going

  • 0

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.

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)

ifstream myfile (path.c_str() , ios::in|ios::binary|ios::ate);
ifstream::pos_type size = myfile.tellg();
cout << size << endl;
fileSize = (int) size;
fileToReturn = new char [size];
myfile.seekg (0, ios::beg);
myfile.read (fileToReturn, size);
myfile.close();

cout << "file read\n"<< fileToReturn << endl;

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?

EDIT: To clarify, my end goal is to send this over the web, not re-save the file.

// 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:44:28+00:00Added an answer on June 13, 2026 at 9:44 am

    The issue is that ostream& operator<< (ostream& out, const char* s ); expects s to be a null-terminated ASCII string. So it stops as soon as it encounters a NUL character. If you really want to write all data to the console, use `ostream& write ( const char* s , streamsize n ), like this:

    cout.write(fileToReturn, size);
    

    The issue is the same for strcat: it stops after the first NUL character. So concatenate using memcpy:

    memcpy(totalReply, reply.c_str(), reply.size()+1);
    memcpy(totalReply+reply.size()+1, fileToReturn, fileSize )
    

    But you tagged this question as C++, so why not do it like this:

    ifstream myfile (path.c_str() , ios::in|ios::binary|ios::ate);
    vector<char> totalReply;
    totalReply.insert(buffer.end(), reply.begin(), reply.end());
    // need a NUL character here?: totalReply.push_back('\0');
    totalReply.insert(buffer.end(), istream_iterator(myfile), istream_iterator());
    send(client, &totalReply[0], totalReply.size(), 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.