   Is There Something Wrong How I Cut Out Pieces of A String? (C++)
  Hello, I want to send data from one PC to another, but for me to send very long data I will have to break up the data in a small  string and send it and then get the next 1024 characters and then send it. //How many strings do we need to send? Max length per string is 1024.   int StringCount = (int)(buf.length() / 1024) + 1;if(buf.length() > 1024){int pos = 0;for(int i = 0; i<StringCount2; i++){std::string tmpBuf;if(pos < buf.length()){tmpBuf = buf.substr(pos, 1024);}else{tmpBuf = buf.substr(pos, buf.length()-pos);}    //Put the file data piece into a buffer that is 1025.sprintf(fileInfo, tmpBuf.c_str()); //Send the piece of data...pos += 1024;}   }else{   //Just send the full string since it's smaller then 1024.   }Does anyone maybe have a better algorithm then mine or that works better? My results on the other side looks that the data skips around.. :(Thanks, Andrew.   
  Use string::size_type for your pos variable, and instead of checking it against .length(), check that it != string::npos. That, or use string::iterator instead.Also, use stringstream instead of sprintf.   ;  It's valid to for the range passed in to substr to extend past the end of the string (in this case the substr operation will end when it reaches the last character) so you don't need that if/else around the call to buf.substr.You could probably reduce that code down to something like://Max length per data sent is 1024.string::size_type size = buf.length();if(size > 1024){for(string::size_type pos = 0; pos<size; pos += 1024){std::string tmpBuf = buf.substr(pos, 1024);    //Send tmpBuf...}}else{//Just send the full string since it's smaller than or equal to 1024.};  Remember that if you have zeroes in that string, they will terminate the string too early and the remaining data will be lost. It's fine when iterating over the string based on its length but it's not fine when you start using c_str() and so on.I also recommend increasing your arbitrary buffer limit from 1024 to something bigger like 8192 or 16384. It is 2010, after all. (Oh, and make it a constant, instead of sprinkling magic numbers everywhere.)   ;  Alright thank you guys!  I got it working I appreciate it! :) Sorry for the long response, but I work everyday so I kinda forget to come back on here to ask or say thanks.   