0

This is an assignment and my professor only allows us to use feof function to read a file input line by line. Now i have an empty file and I got stuck in an infinite loop.

...
while(!feof(stdin)){
    ...
    char inputLineArray[1000];
    scanf("%[^\n]%*c", inputLineArray);
    ...
}
...
kluo
  • 131
  • 1
  • 10
  • "Now I have an empty file" means what? Are you trying to read an empty file? Did this create one? – tadman Sep 18 '18 at 18:48
  • 2
    Send your professor back to school. `while(fgets(inputLineArray, 1000, stdin) != NULL) { . . . }` – Weather Vane Sep 18 '18 at 18:51
  • So how could I fix this? @user3121023 – kluo Sep 18 '18 at 18:53
  • @EfiShtainer `stdin` is a predefined file which is already open. You can use any opened file, but `scanf` is reading `stdin` and `fscanf` is reading the opened file you tell it (which can be `stdin`). Whereas `fgets` always needs a file variable. – Weather Vane Sep 18 '18 at 19:18
  • @user3121023 in which case the `%*c` is not necessary, which consumes one trailing whitespace, however the added space before `%` consumes *all* leading whitespace. – Weather Vane Sep 18 '18 at 19:23
  • `feof` doesn't read any input, so what are you supposed to use to read? scanf? Try using `while(scanf(" %999[^\n]", inputLineArray) == 1) {`... – Chris Dodd Sep 18 '18 at 23:50

0 Answers0