Sure, that would work. But I would do it the way it's done in the original code as it's an idiomatic way to read "something" from "somewhere" for as long as there are more "somethings" to read.
Read one character at a time from stdin until there are no more characters to read:
Code:
int c;
while ((c = getchar()) != EOF) {
// do something with the character
}
Read one line at a time from stdin until there are no more lines to read:
Code:
char buf[1000];
while (fgets(stdin, buf, sizeof buf)) {
// do something with the line
}
Read one directory entry at a time from dp until there are no more directory entries to read:
Code:
struct dirent *ep;
while ((ep = readdir(dp))) {
// do something with the directory entry
}
Note that you can just put parentheses around the expression "ep = readdir(dp)" to tell the compiler that you meant to use "=" as an assignment and not as a comparison (it's just a compiler warning because it's usually a mistake to use "=" instead of "==" in a while or if statement).