Freopen () doesn't work on mac

I am trying to run code with freopen () on mac os, but it does not output the result in the specified file. though, it works fine on windows. I am using X-Code editor and input and output files are in the same path as 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;
}

      

+3


source to share


1 answer


This is because the working directory of your executable is not the directory where you have the files .cpp

.

You can point to your files with absolute paths eg. /Users/omar/Documents/input.in



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

+1


source







All Articles