Visual C++ read entire file -


for days have been trying read entire png string, can upload server via winsock2, appears stop reading file after few characters or sort of line break, there particular reason , way of solving it.

i have tried many many solutions , starting drive me insane. current code using follows

std::ifstream in ("some.png", ios::in|ios::binary|ios::ate ); std::string contents; if (in) {                in.seekg(0, in.end);     contents.resize(in.tellg());     in.seekg(0, in.beg);     in.read(&contents[0], contents.size());     in.close();     length = contents.size();         } 

i have no idea problem be, relatively new c++, have trolled through google days no working solution.

please help

update code posting server

    wsadata wsa;          if (wsastartup(makeword(2, 2), &wsa) != 0)     return;       socket fd = socket(af_inet, sock_stream, ipproto_tcp);              if (fd < 0)                      throw;              sockaddr_in service;      service.sin_family    = af_inet;     service.sin_port      = htons(80);     lphostent host        = gethostbyname("127.0.0.1");              if (!host)                           throw;              service.sin_addr = *((lpin_addr)*host->h_addr_list);              if (connect(fd, (sockaddr *)&service, sizeof(service)) < 0)                      throw;      int length ;      std::ifstream in (ccfileutils::fullpathfromrelativepath("back.png"), ios::in|ios::binary|ios::ate );     std::string contents;              if (in){              in.seekg(0, in.end);         contents.resize(in.tellg());         in.seekg(0, in.beg);         in.read(&contents[0], contents.size());         in.close();         length = contents.size();     }else          std::string str =         "post /index.php http/1.1\r\n"         "host: metapps.co.uk\r\n"         "accept: */*\r\n";          char buffer1 [50];           str.append(  "content-length: 121\r\n" );         str.append(  "\r\n" );          str.append(  "content-disposition: form-data; name=\"tmp\";filename=\"photo.png\"\r\n" );         str.append(  "content-type: image/dds\r\n" );         sprintf (buffer1, "content-length: %d\r\n", length);         str.append( buffer1 );         str.append( contents    );         str.append(  "\r\n\x01a\r\n" );          // send str ...         send(fd, str.c_str() , strlen( str.c_str() ) +1 , 0);         char ret[1024];          recv(fd,ret,strlen(ret),0);          closesocket(fd);         wsacleanup(); } 

update 2

its null terminator string , append method

if do

str.append( "he\0llo"   ); 

the server show "he"

if do

str.append( "hello" ); 

i hello, info, can lead solution

    send(fd, str.c_str() , strlen( str.c_str() ) +1 , 0);         char ret[1024]; 

strlen( str.c_str() ) +1 tell position of first 0 byte in output, , not length of string. length of string best optained via str.size() instead.


also, whoscraig mentioned, you're calling strlen(ret) ret uninitialized. instead, use

  std::array<char, 1024> ret;   recv(fd,ret,ret.size(),0); 

or potentially more dynamic.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -