3

why is the while loop written in this code with no body? what does it do? The index and length are both integers and I am reading from a txt file.

  // ignore alphabetical strings too long to be words
        if (index > LENGTH)
        {
            // consume remainder of alphabetical string
            while ((c = fgetc(fp)) != EOF && isalpha(c));

            // prepare for new word
            index = 0;
        }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
A.Emad
  • 321
  • 2
  • 3
  • 9
  • 4
    The body of the work, is within the `while` statement. It skips alphabet characters from the file. – Weather Vane Aug 09 '16 at 18:31
  • *why?* because there's nothing to do in the loop *what does it do?* what the comment says: consume remainder of alphabetical string i.e. read until EOF or a non-alphabetical character found – stijn Aug 09 '16 at 18:31
  • It's setting 'c' equal to the contents of the file. However, from code shown c looks unused other than being set to the value of the file. – Dominic Aug 09 '16 at 18:32
  • 1
    The fact that this is obvious to old cats doesn't make it a poor quality question. UV'd – Bathsheba Aug 09 '16 at 18:32
  • Note: `int c; ....while ((c = fgetc(fp)) != EOF && isalpha(c));` can simplify to `while (isalpha(c = fgetc(fp)));` as `EOF` is not an alpha and `isalpha(EOF)` is defined. – chux - Reinstate Monica Aug 09 '16 at 21:12

6 Answers6

4

To better understand what's happening, the loop can be rewritten as follows:

do {
    c = fgetc(fp);
} while (c != EOF && isalpha(c)); 

It reads a character from the file descriptor, then checks to see if it is either an alphabetic character or EOF.

What the original loop is doing is performing the assignment as a subexpression within the conditional, so that leave the body blank.

dbush
  • 205,898
  • 23
  • 218
  • 273
4

Once it has parsed string of length LENGTH, it should ignore all the subsequent characters. Thus, in the body of the while loop:

c = fgetc(fp)) != EOF //get c from file pointer and check if it is not EOF
isalpha(c) //and also it is an alphabet because alphabets can only make a word

If both conditions are true, keep parsing and ignoring. Moment you encounter a character other than alphabet or just an EOF, reset index and ma be go to next line.

Sanjeev Kumar
  • 112
  • 2
  • 3
2

why is while written here with no body?

TL;DR - because, it can be written and it serves the desired purpose of the code.

To elaborate, in the code

while ((c = fgetc(fp)) != EOF && isalpha(c));

the && operator and its properties are being utilized as part of the controlling expression which serves the purpose of the loop statement. The condition check in while remains TRUE (1) as long as both the operands of && evaluates to TRUE (1). Once either of them is not TRUE, the loop ends.

Just to be clear, till (c = fgetc(fp)) != EOF and isalpha(c) evaluates to non-zero value (TRUE), the loop coninues (with an empty body).

The main pages of fgetc() and isalpha() may be of further help.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

This while is written with no body, because the action is carried out in the condition part of the while loop.

If you see the condition of the loop it is:

((c = fgetc(fp)) != EOF && isalpha(c))

When first condition - i.e. (c = fgetc(fp)) != EOF - is evaluated, c = fgetc(fp) gets executed, which reads one byte from your file. This byte is first checked if it is EOF, which signifies end of file, and if not EOF then it is checked if it is a non-alphabet character.

This action of reading one byte from the file continues either untill either a non-alphabet character is read, or the end of file is reached.

So, what this snippet of code basically does is, whenever index becomes more than LENGTH, it first reads remaining alphabets - without doing anything -, untill a non-alpbabet is read or the end of file is reached. And then it setsindex to 0.

That while could be written differently too, so that it would not have an empty body. However, what you have seen is a common idiom in C.

sps
  • 2,720
  • 2
  • 19
  • 38
0

The comment clearly mentions the purpose of the code.
// consume remainder of alphabetical string while ((c = fgetc(fp)) != EOF && isalpha(c));

Function fgetc on each invocation reads a character from the file pointer fp. On reading it skips the next sequence of alphabets until it reaches something else ex a digit.

Sridhar Nagarajan
  • 1,085
  • 6
  • 14
0

So if you read the documentation on fgetc it says that it when successful it will return the character currently pointed by the internal file position indicator or EOF. So that while loop is just assigning c to the next character in the file, checking if its not and EOF and making sure its a alpha character and it continues doing that until those conditions are not true. That loop could be re-written to:

int c = fgetc(fp);
while (c != EOF && isalpha(c))
    c = fgetc(fp);
Hedron
  • 86
  • 1
  • 9
  • `fgetc()` returns `int` instead of `char`. For details, read [fgetc](http://www.cplusplus.com/reference/cstdio/fgetc/). As written `while (c != EOF && isalpha(c)) c = fgetc(fp);` can enter into an infinite loop on platforms where `char` is unsigned. – chux - Reinstate Monica Aug 09 '16 at 21:15