2

I need to make a game in C for my finals. The user should input the map file he wants to play.

Here's my simple code:

int main(){

FILE *map;

char fileToRead[100];

do{
    printf("Insert file name: ");
    fgets(fileToRead, 100, stdin);

    map = fopen("/Users/rajunior/Desktop/map_2.txt", "r");
    //map = fopen(fileToRead, "r");
    printf("%s", fileToRead);

If I use the "map = fopen("/Users/rajunior...)" hardcoded, it works! But I need to use the second (commented) option; the first one is useless for my purpose.

In other words, I need the fileToRead to be in the same directory as my .c, but how?

screenshot: https://i.sstatic.net/Ar426.jpg

2 Answers 2

2

Option 1: Install the command line tools. Put the C file and the text file in the same directory. Open a terminal window. Compile and run from the command line. If I recall correctly, the command line tools download can be found in Preferences.../Downloads.

Option 2: Go to the Product/Scheme/Edit Scheme... menu. When the dialog box appears, select Run at the left and Options at the top. Then look for Working Directory. Set the working directory to point to the directory where the text file is.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your time. The option 2 I already tried and it didn't work. I printed the directory through getcwd() and it was right, but the file never opened. I installed the command line tools and I'm trying to compile and run through terminal with: $ gcc read_files.c -o readFilesRun, but when I run it, it doesn't work either. I really don't know what to try anymore. Thinking about installing some sort of virtual machine and finish my task on windows. :(
Thank you man, the second option worked for me like a charm! Best regards, Denny
1

This was going to be a comment, but it is too long for comfort.

You'll need to know the current directory of the process when it is run. If you run it from the shell, the current directory of your program will be the same as the current directory of the program. If you run it from within XCode, I've no idea what the directory will be, but it probably won't be where the source is — it'll be in a build directory of some sort, probably.

Your program can find out where it is run from with getcwd(). Then you'll be able to tell how to chdir() to the directory where the source is (as long as the program knows where the source is, because you told it somehow — argument or command line variable, or …). Or you can determine how to create a relative path name that will find the file in the source directory.

There's probably an XCode (maybe Objective-C) way to find the information, perhaps via plists.

I don't code for a Mac; I only code on a Mac, and I run XCode itself rather seldom.

2 Comments

Thank you! I already tried it. It's weird 'cause when I print the directory, it appears to be right, but when I try to run the program and read the file, it never reads! I really don't know what to try anymore.
Oh — fgets() includes the newline; you need fileToRead[strcspn(fileToRead, "\n")] = '\0'; to zap the newline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.