Matthew Brzezinski
Matthew Brzezinski

Reputation: 1794

Check to see if file input line is empty

I have a text file in the following format:

Some information here

Some more information here

I want to check to see if the inputted line is blank (line 2 above). I've tried various things but none of them seem to be working, there's obviously something simple that I am missing here.

void myFunc(char* file_path) {
    FILE* file;
    char buff[BUFFER_SIZE];
    file = fopen(file_name, "r");
    bool flag = false; 

    while(fgets(buff, BUFFER_SIZE, file) != NULL) {
        if(buff[0] == '\n') {
            flag = true;
        }
    }
}

I've tried strlen(buff) == 0, strcmp(buff, ""), buff[0] == '\0' and many other things but I still can't seem to be getting this to work properly.

Upvotes: 2

Views: 41

Answers (1)

R Sahu
R Sahu

Reputation: 206737

It's possible that the second line has more than just the newline character.

You can use a helper function to test that out.

void printDebug(char* line)
{
   char* cp = line;
   for ( ; *cp != '\0'; ++cp )
   {
      printf("%d ", (int)(*cp));
   }
   printf("\n");
}

By examining the integer values of the characters printed, you can tell whether the line has more than one character, and what those characters are.

Upvotes: 3

Related Questions