Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jsmwoolf

macrumors regular
Original poster
Aug 17, 2011
123
0
I've ran into this problem before while learning C, but I'm trying to solve a problem on Project Euler(no surprise) that requires the user to open a file and read data off. I haven't wrote an algorithm on how to solve it, but the problem is that somehow, I can't open a file using fopen or write to a file. So far I have this code to simply try to open the file:

Code:
#include <stdio.h>

int main (int argc, const char * argv[])
{
    FILE *list;
    list = fopen("roman.txt", "r");
    if(list==NULL)
    {
        printf("Error! Can't find file!\n");
        return 1;
    }
}

I tried to even add the txt file to the workspace, but to no success. The file is with the directory and I'm writing the actual project on a USB drive. The question is what's going on and why doesn't it work?
 
Last edited:
I've ran into this problem before while learning C, but I'm trying to solve a problem on Project Euler(no surprise) that requires the user to open a file and read data off. I haven't wrote an algorithm on how to solve it, but the problem is that somehow, I can't open a file using fopen or write to a file. So far I have this code to simply try to open the file:

Code:
#include <stdio.h>

int main (int argc, const char * argv[])
{
    FILE *list;
    list = fopen("roman.txt", "r");
    if(list==NULL)
    {
        printf("Error! Can't find file!\n");
        return 1;
    }
}

I tried to even add the txt file to the workspace, but to no success. The file is with the directory and I'm writing the actual project on a USB drive. The question is what's going on and why doesn't it work?

Are you getting an error message? If so, what is it? Does the code compile? Do you get the "Error! Can't find file!" message?
 
You need to add the file to the directory where your executable file is, or better provide a path to the file in the first argument.
 
You need to find out what the actual working directory is when your program runs.

This code uses a relative pathname:
Code:
    list = fopen("roman.txt", "r");
"Relative" means "relative to the working directory". So find out what the working directory actually is, and work things out from there. By that, I mean either put the file in the right working directory, or figure out how to change the working directory when your program runs. Since you haven't said exactly how you're running your program, nor exactly where the file resides relative to your running program, it's difficult to be more precise with only the information you've posted.


To find out what the working directory is, use the getcwd() C function. It has a man page, which should be readable in Xcode (look under its Help menu).

It's definitely readable in Terminal (man getcwd) and on the web:
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/getwd.3.html
 
  • Like
Reactions: karmaman
Oh, I guess I wasn't specific. Yes, I do get the printf prompt when executing it. I have actually found the directory, placed it, and it now works, but how can I change the directory to the project folder?
 
Edit your scheme. Example picture:

scheme_run.png


Notice the "Working Directory" option, and the "Use custom working directory" checkbox. Change that.

If you don't know how to edit schemes, you should probably spend a little time now learning how to configure and manage schemes. It's likely to come up repeatedly for any non-trivial use of Xcode 4.

There are online reference docs for Xcode and schemes. I'm pretty sure Xcode still has a Help menu, too.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.