Question

#include <cstdio>
#include <iostream>
#include <fstream>
#define INPUT_FILE

#ifdef INPUT_FILE
    freopen("test.txt", "r", stdin);
#endif

using namespace std;

int main(int argc, char const *argv[])
{
    int n;
    while(scanf("%d", &n))
        printf("%d\n", n);
    return 0;
}

I'm trying to pass input to the program via an input file but, the following error pops up,

error: C++ requires a type specifier for all declarations
freopen("test.txt", "r", stdin);
    ^~~~~~~
1 error generated.
Was it helpful?

Solution

You can't use a function outside of a function or any other executable part of the program.

Your program is equivalent to

#include <cstdio>
#include <iostream>
#include <fstream>
#define INPUT_FILE


freopen("test.txt", "r", stdin); // Makes no sense

using namespace std;

int main(int argc, char const *argv[])
{
    int n;
    while(scanf("%d", &n))
        printf("%d\n", n);
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top