2

I have a text file that looks like this:

NAME:333
VALUE:555

TEST:355

REST:224
   4444

I'm trying to check ahead for empty lines, and if found skip them (don't parse them), I'm also trying to check for white space (line wrapping). However, my code doesn't exactly work as it should:

int main(void) {
    FILE*ptr;
    ptr = fopen("test.txt","r");

    if(ptr == NULL)
        return 0;

while(fgets(buffer,sizeof(buffer),ptr) != NULL) {

    if(buffer[0] == "\n") //blank line, skip it
        continue;

    else {
        if(strcmp(buffer,"\r\n") == 0)
        printf("Blank Space found\n");
    }


}



return 0;
}

However, what's happening is that when a blank line is found, the else statement executes, instead of the if. (On a Ubuntu system ). So I'm not sure how to differentiate between completely empty lines, and lines with white space that possible wrap to the next line.

I also tried using strcmp to check for a blank line (explicitly in the if statement), but that never executed, even though there are 2 blank lines in my text file, any help would be greatly appreciated.

  • 1
    You'll find severai good alternatives here: [Removing trailing newline character from fgets() input](http://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input). My favorite is `s[strlen(s) - 1] = '\0';`. Note that it has limitations. Note, too, that you'll need to perform extra checks if there are tabs or spaces preceding EOL. – paulsm4 Jan 26 '16 at 23:36
  • So SImply isspace(buff[0]) or at every index? – TestSubject201 Jan 26 '16 at 23:41
  • 1
    `for (int i=0; i < strlen(s); i++ { if (isspace(s[i])) ... }` – FoggyDay Jan 26 '16 at 23:43
  • 3
    `if(buffer[0] == "\n")` that needs to be `if(buffer[0] == '\n')`. That is, you need to compare a `char` not a string (strings cannot be compared like that anyway). But to address all your requirements is going to require more logic than just that. – kaylum Jan 26 '16 at 23:43

0 Answers0