I'm running into a weird bug in Visual Studio Code - I'm writing code in C using standard extensions (C/C extension pack) and if I write a simple program like this, it works fine:
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout << "Hello World!" << endl;
}
However, the second I declare a variable using STD include statements, such as a map
, vector
, etc., the code runs without errors but doesn't print anything.
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
vector<int> test;
cout << "Hello World!" << endl; // this no longer prints
}
Any ideas why this error is occurring? I have all the include statements I need as well:
#include <bits/stdc .h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
CodePudding user response:
I tested your code, and it ran as expected.
Some ideas:
- When
ONLINE_JUDGE
is not defined,freopen("output.txt", "w", stdout);
makes the output redirected tooutput.txt
. Maybe you forgot checking the file instead of the console the second time you ran your program. - Besides, since you were using Visual Studio Code, maybe you used an extension to run your program. The extension may not use your current working directory to run the program, which made the
output.txt
elsewhere. Also, check ifONLINE_JUDGE
is set by the extension or your script.
BTW, if you use g , #include <bits/stdc .h>
will include all headers of standard library, and there is no need for including other headers. But doing so will dramatically increase your compilation time.