1
votes

My program is using while scanf != EOF to input standard inputs from a .in file. as in ./"file" < file.in The problem is that I have to scanf from the terminal after that. Is there a way I can invoke EOF to the stdin so that the file stops scanning from file.in or maybe specify scanf to read from the terminal and not the file. I'm also only allowed to use scanf as this is a college assignment. This is a mock program of my issue. Thanks

int main() {
int arr[100];
int num;
int count = 0;
while( scanf("%d", &arr[count]) != EOF ) {
    count++;
}
printf("%d\n", arr[0]);
scanf("%d", &num);
printf("%d\n", num);
}

** Output is just the number of the first list of number, and scanf is completely ignored with no value in print num.

2
en.wikipedia.org/wiki/End-of-file ctrl-d in linux, ctrl-z in windows - clcto
Your while doesn't do what you expect. - edmz
sorry so do i just printf of a command ctrl-d? Sorry i'm new to c - Sarc
@Sarc , Press CTRL+D - Spikatrix
but it has to be in the code, because the input data comes first and no prompt for user input comes up - Sarc

2 Answers

0
votes

scanf() return number of read chars. you need feof() function, or compare returned value from scanf with 0

0
votes

To invoke/simulate EOF in stdin , press CTRL+Z if you are in windows. Else press CTRL+D when running the program.