So we are learning about scanf and redirection of input. I have to read in the data from stdin and store it in an array of a predetermined size. Afterwards we have to ask the user for an integer x and do some basic searching for that integer in the array. The user at the command line must use "./a.out< somefile.txt" to provide the data. I am reading this data via scanf and I have used a while loop that terminates when scanf reads EOF. The problem is when I want to re-use scanf to ask for user to enter the integer x to search for in the array. scanf always returns -1 and stores 0 in x, so I can't seem to use scanf. I am confused by the activity of scanf. Below I provide a simple code of my program and a sample .txt file.
data.txt file that user enters at command line via redirection "<"
1
2
3
4
5
6
Now my sample program
int main(){
int arr[6], i = 0, value, x;
while(scanf("%d",&value) != EOF){
arr[i] = value;
i++;
}
printf("Enter integer to search for: ");
scanf("%i", &x);
printf("You entered %i", x);
return 0;
}
Now this is the output, it just runs and does not wait for user to input data
Enter integer to search for:
You entered 0