当前位置: 动力学知识库 > 问答 > 编程问答 >

c - Search for a newline charactor or CR/LF characters

问题描述:

I am searching a string and the string could contain a newline or a CRLF. I increment the pointer to search through each character. I have converted the hex to decimal. However, as I am looking for a single char, I am not sure this would be the best way to do this.

Many thanks for any suggestions,

 /*

0x0a (ASCII newline)

0x0d (ASCII carriage return)

CRLF (0x0d0a)

*/

while(*search != '\0') {

/* Seach for a newline */

if(*search == 10) {

printf("\nnewline Found\n");

}

/* Search for a CRLF */

if(*search == 3338) {

printf("\nCRLF Found\n");

}

search++;

}

I'm compiling with gcc 4.6.2 in C89-mode.

网友答案:

CRLF is two characters: CR, and LF. You need to match them both individually, not together as you've done with 3338 (I think: where did you get that number?) Also, use the character constants: '\0', '\n', '\r', it will make your code clearer.

/*
  0x0a (ASCII newline)
  0x0d (ASCII carriage return)
  CRLF (0x0d0a)
*/

while(*search != '\0') {
    /* Seach for a newline */
    if(*search == '\n') {
        printf("\nnewline Found\n");
        search++;
    }

    /* Search for a CR or a CRLF */
    if(*search == '\r') {
        // OK, we found a CR, is it followed by a LF?
        if(*(search + 1) == '\n') {
            // Yes, it is, thus, it is a CRLF
            printf("\nCRLF Found\n");
            search += 2; // Note the 2! CRLF is 2 characters!
        }
        else {
            // No, just a lonely CR, forever alone.
            printf("\nCarriage return found\n");
            search++;
        }
    }
}

As another poster suggests, you can also use the strchr and strstr functions, and take the minimum valid result from them. (It'll be like 3 lines of code.)

网友答案:

Look up the strchr and strstr functions. They've been made for this.

But CRLF is not one character, that's two characters. To locate that, search for '\n' and check the previous character.

网友答案:

You could simply modify your code to:

for(; *search != '\0'; search++) {
    /* Seach for a newline */
    if(*search == '\n') {
        printf("\nnewline Found\n");
    }
    /* Search for a CRLF */
    else if(*search == '\r' && *(search + 1) == '\n') {
        printf("\nCRLF Found\n");
        search++; /* search will be incremented twice (bit hacky?) */
    }
}

Short-circuiting in the if statement will prevent (search + 1) being dereferenced if *search is not '\r', so that won't cause you to go past the bounds of the string if *search turns out to be '0'.

*search == 3338 in your previous code example isn't going to work if search is a char*/char[], since a single char can only represent 256 different values and CRLF is a sequence of 2 bytes (2 chars).

网友答案:

I'd recommend to use strstr() and not to use strchr(), as the latter is not multi-byte character save (per definition).

Also the two methods strspn(), strcspn() could help.

网友答案:

personally i'm doing it like this for the time being with the test stub i'm using ~

..mainly because I just get to use the modulo operator :D

int count(char *s) {
    char *p = s; 
    int i = 0;         
    while (*p) { 
        if (*p == '\r' || *p == '\n') { 
            i++;
        }
        p++; 
    }        
    if (i % 2 != 0) { 
            fprintf(stderr, "%s", "\n\x1b[31mgooby pls?\x1b[0m\n");
            dosomething;  
    }
    return i / 2; 
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: