2

I am new to C and was wondering how I could do something like this:

I have a string coming in like this through a socket (it could vary)

"GET OK 1045 \r\nTHE RED RIDING HOOD"

Now I may get it in chunks of it and not completely like

"GET OK 10"

And later on

"45 \r\nTHE RED RIDING HOOD"

What is the best way to split when it finds the \r\n and store what came before it in a variable and what comes after it in another one.

I have tried strtok() but it just became way to messy and was getting errors

I tried sscanf() but since i may be getting chunks I can never get it to format it correctly into my variables. I am sure there my be a easy way to do this in a while loop but the splitting is what is getting me.

 while((items_read = read(sock, stories_buffer, BUF_SIZE)) > 0) {
     //how do i check on stories_buffer as it comes and split it based on delimiter \r\n
 }
5
  • 1
    strtok is exactly the way to go. why did it got messy? I think it is best to wait for all parts, concatenate them together and go with strtok. Commented Jan 20, 2020 at 13:50
  • 2
    Did string manipulation get messy in C? Well, that's just the way it is. :) Commented Jan 20, 2020 at 13:51
  • lol thanks will go with it then (rolling eyes) :) Commented Jan 20, 2020 at 13:59
  • Some ways are better than others, but string manipulation in C will never be as smooth as in Python. Commented Jan 20, 2020 at 14:01
  • @IgalS. strtok doesn't help if you don't know how much to read. Can't just "read the entire file" with many network protocols, you will sit on read/recv forever while the other end expects a response. Commented Jan 20, 2020 at 14:03

1 Answer 1

2

You can wrap the socket into buffered IO and use the stdio functions like fgets() on it:

FILE *f = fdopen(sockfd, "w");
fgets(buf, sizeof(buf), f);

This should be the best option for line-based network protocols.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you Ctx, would this be within the while loop after reading? and also how would i still detect the \r\n delimiter?
Of course you can loop over the fgets(), which already reads only up to the next newline. You just have to strip the newline (and perhaps the carriage return) if you do not need that. You just have to make sure that the buffer is large enough; if it is not, then the message will be truncated and you have to enlarge the buffer and call fgets() again to read in the remaining bytes.
I see, yeah I need to store whatever is after the \r\n some where else for further processing and at the same time keep an eye on what i am getting for what's before the \r\n
@DunkRuZ As I said, fgets() only reads up to the next \r\n and stops then. A subsequent call to fgets() will then fetch the next line. So there will be nothing after a \r\n.
Shouldn't this be "r"?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.