Hello, I am trying to move data from a client to a server, and save it in a file. However, I am getting a "Runtime Error!" when I use the substr() function, here:

char buffer[256];
int sr = recv(acc, buffer, sizeof(buffer), 0);
string buffStr = buffer;
outfile << buffStr.substr(9,buffStr.size()-1) << "\n";

Once the first line in the file is sent, though, the runtime error is caused in the server, and it occurs only when the substr() function is used. I also have tried "buffStr.size()", "buffStr.length()", and "buffStr.length()-1".

Sorry if its another one of my stupid questions; I seem to be real good at finding them.

> int sr = recv(acc, buffer, sizeof(buffer), 0);
1. recv doesn't store a \0 on the end to make it a proper string
2. You need to leave room to store the \0 yourself
3. You need to code for the possibility that recv() doesn't receive the whole message in one go (if this is a TCP connection)

int sr = recv(acc, buffer, sizeof(buffer)-1, 0);
if ( sr > 0 ) {
  buffer[sr] = '\0';  /* make it a string */
  /* now you can do str... operations on buff */
}
commented: Agree:sunny +1

The error still occurs:

sr = recv(acc, buffer, sizeof(buffer)-1, 0);
cout << "sr = " << sr << "\n";
buffer[sr] = '\0';
buffStr = buffer;
outfile << buffStr.substr(9,buffStr.size()-1) << "\n";

The output of sr is 1, which is not the size of the buffer, hopefully, since the first line is "I want to".
This error occurs every time, so it is not that it is getting lost that is the error, I do not think.

> The output of sr is 1, which is not the size of the buffer
Who said it would be?
The size you pass is "don't go past this many characters", not "please hang around until you fill the buffer".
You got 1 character, so copy it to somewhere else, and call recv() again.
Keep calling it until you have all your data.

Well, the statement

buffer[sr] = '\0'

seems to say that sr is the final character. The buffer is sent 1 line at a time, so there shouldn't be just 1 character in the buffer, because there is no line with only 1 character in it.

K, figured it out. The first line was sending a blank line. This caused the error because there weren't enough characters. So I just told it to ignore the first line, and it is working fine.
But, I didn't need to add a null pointer to the end of the string. It works anyways.
Thanks for helping, and sorry for bothering you with something that I had mistaken to be erroneous.

> But, I didn't need to add a null pointer to the end of the string. It works anyways.
1. It's a NUL char, not a NULL pointer.
2. Yes, you do need to add it, otherwise you're relying on the buffer being initialised (which it isn't).
On the other hand, you could be sending the \0 (either by accident or by design).

Wanna see?
Try

memset( buffer, 0xff, sizeof(buffer);  /* very specific non-zero fill */
sr = recv(acc, buffer, sizeof(buffer)-1, 0);
cout << "sr = " << sr << "\n";
buffStr = buffer;

Now see whether buffStr is correct or not?

Sorry, I guess I wasn't thinking when I wrote 'null pointer.'
Is it possible that either the "getline(ifstream, string)" method returns a null character at the end or "string.c_str()" does? Then it could, as you said, send the null character through as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.