문제

#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.
도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top