2

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?

jinglei
  • 3,269
  • 11
  • 27
  • 46

2 Answers2

3

The relative path is unrelated to where the program source live. It's actually relative to "current working directory" (basically, type pwd in your shell window to see what your working directory is, then run the program, if there's a file in ../data.in, it will work, otherwise you'll probably get all sorts of weird errors).

You should also check the return value from freopen, if it returns NULL, an error occurred and you should probably deal with that.

Vatine
  • 20,782
  • 4
  • 54
  • 70
  • I've edited the question and added my project structure. I've tried to use `pwd` to help me work it out but in vain. I don't think there's anything wrong with the path because the file `data.in` does exist in the parent directory of `main.c`. Could you please give me any hint? – jinglei Apr 14 '16 at 11:05
  • Well, I've figured it out. It is the working directory that set by `Xcode` that lead to the issue. Just like @purplepsycho mentioned. In Xcode the default debug working directory is `~/Library/Developer/Xcode/DerivedData/project-/Build/Products/Debug`. After I customized the working directory, everything worked fine. – jinglei Apr 14 '16 at 11:15
2

To use a relative path, you must know what is the current directory of your running process.

See getcwd().

If you are launching the program from a command line, the current directory is the same as the command line one. In command line (sh, bash...) the command pwd will tell you where you are.

If you launch the program from a debugger, the debugger will choose the program starting directory, this should be an option of your debugger.

Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • Just like you said, Xcode changes the debug working directory to `~/Library/Developer/Xcode/DerivedData/project-/Build/Products/Debug`. Customizing the working directory makes everything work correctly. – jinglei Apr 14 '16 at 11:17
  • You can refer to http://stackoverflow.com/questions/3396378/change-the-working-directory-in-xcode for changing working directory – Saleh Enam Shohag Oct 01 '16 at 21:06