0

I'm writing a program that is essentially emulating the linux GET command, which will return a msg that contains a file and the header separated by \n\n inside the message. The only thing is I'm not sure how to search the returned string and find this message, because \n signifies that a string has ended. If anyone can help lead me on the correct path that would be awesome.

4
  • Are you sure it's \n\n and not \r\n?
    – Brandon
    Apr 6, 2014 at 16:12
  • I'm just going by what the professor shared with us, he said it was \n\n, but it might be \r\n, I'm just not sure how to read those characters in a binary file, or do a comparison with them.
    – Ranma344
    Apr 6, 2014 at 16:15
  • @CantChooseUsernames \n\n is also a common used pattern, for example in the return of a GET HTTP statement
    – user287107
    Apr 6, 2014 at 16:23
  • Ok, so it is \n\n that I'm looking for. How to I read that in a returned message? Because that is exactly what I'm doing.
    – Ranma344
    Apr 6, 2014 at 16:26

1 Answer 1

1

In C, assuming you are talking about zero terminated strings (the norm), \0 (i.e. the NUL character, i.e. a zero) indicates a string has ended, not \n.

You can search for two \n using the strstr function. From the man page:

   #include <string.h>

   char *strstr(const char *haystack, const char *needle);

So something like:

char *found;
found = strstr (string_to_search, "\n\n");
2
  • The thing is, this should be a binary message that I am comparing, which means that there is a chance it could contain the value for \0, which would break this process would it not?
    – Ranma344
    Apr 6, 2014 at 16:51
  • Sure, in which case use memchr and check that the next character is also \n.
    – abligh
    Apr 6, 2014 at 20:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.