I'm learning to use freopen()
to redirect stdin to a file.
I'm under OS X 10.10
and I'm using Xcode to run my code.
Here's my code:
#include <stdio.h>
int main(int argc, const char * argv[]) {
freopen("../data.in", "r", stdin);
char a;
scanf("%c", &a);
fclose(stdin);
printf("%c", a);
return 0;
}
EDIT
I use tree
command in terminal to show file structure and the output is
.
├── competition
│ └── main.c
├── competition.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── competition.xccheckout
│ │ └── xcuserdata
│ │ └── yang.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ └── yang.xcuserdatad
│ ├── xcdebugger
│ │ └── Breakpoints_v2.xcbkptlist
│ └── xcschemes
│ ├── competition.xcscheme
│ └── xcschememanagement.plist
├── data.in
└── makefile
I've also check the output of freopen()
. The function returned NULL
.
I got nothing while I got correct output when I changed "../data.in"
to absolute path.
How can I fix it using relative paths?