Like Tree1Likes
  • 1 Post By Adak

Search for Blank Line

This is a discussion on Search for Blank Line within the C Programming forums, part of the General Programming Boards category; I have a block of text (http headers for anyone who is familiar) and would like to print all text ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    10

    Search for Blank Line

    I have a block of text (http headers for anyone who is familiar) and would like to print all text before a blank line occurs (since an http header ends with a blank line). The first thing I do is use strstr() to find a keyword, in this case it is "Host:". Then I would like to search for the blank line and print all data in between the keyword and blank line. I have not been able to find a certain character that defines a blank line or an easy way to use a function to find the blank line. Does anybody have a suggestion for the easiest way to do so? I do not want to have to go through every line until a "/r" appears twice in a row.

    Example:

    (1)Text..........
    (2)Host:.........
    (3)Text..........
    (4)Text..........
    (5)Text.........
    (6) "Blank Line"
    (7)Text.......
    (8)Text.........

    I would like to print from (2) to (5) (or (6), it doesn't matter) in this case.

    Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    About the only way is to look for two newlines in a row... one at the end of the preceding line and one alone on the blank line.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Take a look at this link. Specifically look for isspace() and isblank().
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    I'm trying to perform this task without iterating through all the characters. Using those functions I would have to check each character to see if it is blank. In this case, I would like to find the next blank line. This is not a blank space per say. I guess I am looking for a way to search for a back to back "\r\n" "\r\n" without going through each character. Thanks.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Do you have a big block of text which is always guaranteed to contain the header?
    How are you fetching the data? In extreme cases, each recv() call might fetch only a few bytes.

    Anyway, assuming you have a large block, then perhaps
    p = strstr(block,"\r\n\r\n");
    if ( p ) p += 2;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by uconnhuskies View Post
    I'm trying to perform this task without iterating through all the characters. Using those functions I would have to check each character to see if it is blank. In this case, I would like to find the next blank line. This is not a blank space per say. I guess I am looking for a way to search for a back to back "\r\n" "\r\n" without going through each character. Thanks.
    Look up strchr() ...

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    while(fgets(mybuffer, sizeof(mybuffer), myfilepointerName)) {
       if(mybuffer[0] == '\n');
      //you found a blank line
    }
    I haven't tried this, but it should work.
    CommonTater likes this.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by uconnhuskies View Post
    I have not been able to find a certain character that defines a blank line or an easy way to use a function to find the blank line. Does anybody have a suggestion for the easiest way to do so? I do not want to have to go through every line until a "/r" appears twice in a row.
    It'd be a piece of cake if the regcomp() and regexec() functions are applied.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suppose this whole thing boils down to what you consider to be a blank line. Is it just a newline following another? Or is a line full of spaces and tabs followed by a newline considered blank too?
    Code:
    if char is an EOL marker
        while isspace char
            if char is an EOL marker
                blanklinecounter++

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Thanks for your help. I think I overcomplicated it a little bit. What it boiled down to was people not using proper protocol for their headers. CRLF was sometimes misused or switched causing some problems when searching it, so I had to add a few extra statements and handling for improper protocol. Thanks for all your help though, it got the job done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiline string, endl, \n, blank line space
    By jackson6612 in forum C++ Programming
    Replies: 9
    Last Post: 04-20-2011, 08:50 AM
  2. Reading a file containing a blank line
    By maniac123 in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2011, 10:42 AM
  3. Replies: 3
    Last Post: 04-27-2005, 11:50 AM
  4. String Search with command line arguments
    By goron350 in forum C Programming
    Replies: 5
    Last Post: 11-29-2004, 04:56 PM
  5. testing for blank line
    By rippascal in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2002, 08:50 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21