4

I am trying to run a code with freopen() on mac os, but it doesn't print any output in the specified file. although, it works perfectly on windows. I am using the X-Code Editor and the input and the output files are in the same path as the cpp file

#include <cstdio>
int main(){
    freopen("input.in","r",stdin);
    freopen("output.out","w",stdout);

    int x;
    scanf("%d",&x);
    printf("%d\n",x);

    return 0;
}
Omar
  • 735
  • 3
  • 9
  • 19
  • 2
    Start with checking the return value to see if opening succeeded. If you're running from within XCode, you need to set the project's working directory to the appropriate location. – molbdnilo Dec 15 '14 at 21:21

2 Answers2

4

That's because the working directory of your executable is not the directory where you have your .cpp files.

You may point to your files with absolute paths, ex. /Users/omar/Documents/input.in

or change the working directory from xcode settings (see Change the working directory in Xcode )

Community
  • 1
  • 1
Paolo
  • 14,386
  • 26
  • 64
  • 85
-1

I personally tried changing the working directory, but it still didn't work... Later I find out that if you compile your c++ file using Terminal, there would be no trouble at all!

To compile c++ using Terminal, type in these commands:

  1. cd yourFileDirectory
  2. g++ -o yourFileName yourFileName.cpp (this will compile your code)
  3. ./yourFileName (and this will run the code successfully!)
Jayendran
  • 8,189
  • 5
  • 47
  • 93
L.R.Z
  • 1
  • 1