0

Today I installed Sublime Text 3 and compiled a C++ file. After compiling, I got an error that said: "Bad file descriptor." I read posts with similar errors, but I can't seem to find a solution.

Here's my code:

#include <bits/stdc++.h>
using namespace std;
#define SYNC ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define alln(x, n) (x) + 1, (x) + 1 + (n)
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define times_used                                                             \
  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"        \
       << "\n"
#define task " "
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }

const int N = 5005;
ll a[N], n, res = 0;
int main() {
  SYNC;
#ifndef ONLINE_JUDGE
  freopen("/home/steven/temp/input.txt", "r", stdin);
  freopen("/home/steven/temp/error.txt", "w", stderr);
  freopen("/home/steven/temp/output.txt", "w", stdout);
#endif
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n - 2; i++) {
    int k = i + 2;
    for (int j = i + 1; j <= n - 1; j++) {
      while (a[k] < a[i] + a[j] && k <= n)
        k++;
      res += k - (j + 1);
    }
  }
  cout << res;

  return 0;
}
Julia
  • 1,950
  • 1
  • 9
  • 22
steven_Q
  • 3
  • 2
  • Do you get the same error if you run the compiled program outside of sublime text? – Botje Aug 07 '20 at 14:41
  • https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Gaurav Goswami Aug 07 '20 at 14:42
  • You should check that the `freopen()` calls succeeded. – Barmar Aug 07 '20 at 17:24
  • 3
    Your code is **write-only**. Get rid of all the `#define`s, get rid of `#include `, get rid of `using namespace std;`, get rid of one-letter identifiers, indent it 2-4 spaces per block, then maybe someone will want to read it, – n. m. could be an AI Aug 07 '20 at 17:25
  • 3
    My guess is `input.txt` doesn't exist. – Barmar Aug 07 '20 at 17:25
  • 2
    I doubt the problem has anything to do with Sublime Text. – Barmar Aug 07 '20 at 17:26
  • @Botje I have tried in Codeblocks and it didn't have same error. I think g++ which is in my Ubuntu laptops , has that error. – steven_Q Aug 08 '20 at 14:48

0 Answers0