2
votes

I have to input a .in file into my program.gcc ./"file" < file.in
Because there are more than one input I have to use the While loop EOF, but because of that I can't scanf again. Here's a mock version of my program. Thank you

#include <stdio.h>

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);
}
1
What is 'more than one input' - more than one number (or more than one line of numbers) in an input file or more than one file to read? - CiaPan
oh sorry i didn't that clear, just more than one line of input. So a list of numbers. - Sarc
And what results does your program produce? Scanf returns number of data fields read and assigned. In this case - the number of int values. So it will be either 1 on success or 0 on failure. Or it can be EOF if a reading error or end-of-file happens. See cplusplus.com/reference/cstdio/scanf - CiaPan
@CiaPan OP certainly wants to read all the input from a re-directed stdin and then wants to to continue reading from the console (non-re-directed input). It is not so much of what the program sample produces as much as how it can consume input from re-directed stdin and then "not-re-directed" stdin. - chux - Reinstate Monica
the input file works and prints, but it doesn't allow for user input in terminal after that - Sarc

1 Answers

1
votes

try this

freopen("con:", "r", stdin);//this for windows. "/dev/tty" for *nix ?
scanf("%d", &num);