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

  • SEARCH
  • Home
  • 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 8788621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:11:17+00:00 2026-06-13T22:11:17+00:00

std::ifstream file(localPath.c_str(), std::ifstream::binary); file.seekg(0, std::ifstream::beg); while(file.tellg() != -1) { char *p = new char[1024];

  • 0
std::ifstream file(localPath.c_str(), std::ifstream::binary);

file.seekg(0, std::ifstream::beg);

while(file.tellg() != -1)
{
    char *p = new char[1024];

    bzero(p, 1024);
    file.read(p, 1024);

    printf("%ld\n", file.gcount());

    n = send(fd, p, strlen(p), 0);
    if (n < 0) {
        error("ERROR writing to socket");
    } else {
        printf("---------%d\n", n);
    }

    delete p;
}

file.close();

Actually the image which I m trying to send is png (size: 27892bytes) As far as reading is concerned every byte is being read properly. But, while writing them into socket only few bytes are being written. Need help on this.

Thanks in advance. 🙂

  • 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-13T22:11:18+00:00Added an answer on June 13, 2026 at 10:11 pm

    strlen() expects a NULL terminated string, not binary image data, when you use strlen() it stops at the first NULL (or zero) byte and the image data could contain zero at the first or second byte or anywhere, so you can’t use strlen() to find the size of the image buffer. you should use the buffer size instead:

    n = send(fd, p, 1024, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

std::ifstream sr(path.c_str()); if (!sr) throw runtime_error(Could not open file '+path+\'); sr.seekg(0, ios::end); streampos lastPos
The usual way to read a file in C++ is this one: std::ifstream file(file.txt,
I am reading a file like this: char string[256]; std::ifstream file( file.txt ); //
How would I do this using STL algorithms? std::ifstream file(filename); std::vector<unsigned char> buf; for(auto
I have code like this std::ifstream file(filename, std::ios_base::in); if(file.good()) { file.imbue(std::locale(std::locale(), new delimeter_tokens())); for(auto&
I'm trying to update a random-access binary file using the std::iostream interface with separate
i have the next code: std::string line; std::ifstream myfile (text.txt); if (myfile.is_open()) { while
I'm relatively new to C++ and I'm trying to read a proprietary file format.
In a C++ program, using std::ifstream, I'm attempting to open a user-specified file --
int main() { std::deque<std::string> mydeque; std::back_insert_iterator<decltype(mydeque)> myback_insert_iterator(mydeque); std::ifstream myifstream(test.txt); while(std::getline(myifstream, *myback_insert_iterator)) { } }

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.