0

I'm using a new mac with macOS Monterey and the M1 Max chip. I'd like fopen to default to the current directory when no path is given (this is what I'm used to and I think is the norm), but it's defaulting to my user folder. So, for example, when I compile the below code, the resultant app, regardless of where it is, will only open file.txt if that file is in my user folder (I get a segmentation fault if it's anywhere else). Similarly, if I have the code make a new file with FILE* fp = fopen("new file.txt", "w");, the new file will appear in my user folder. I'm compiling with gcc (that I had downloaded via downloading the newest version of Eclipse) from Terminal with the command:

gcc -std=c99 main.c -o yo

When I build using Eclipse or leave out -std=c99 I have the same issue.

Also, for gcc --version I get:

InstalledDir: nathanschmidt@nathans-MacBook-Pro ~ % gcc --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/4.2.1 Apple clang version 13.0.0 (clang-1300.0.27.3) Target: arm64-apple-darwin21.3.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    FILE* fp = fopen("file.txt", "r");
    printf("%d\n", fp);
    int x;
    fscanf(fp, "%d", &x);
    fclose(fp);
    printf("%d\n", x);
    return EXIT_SUCCESS;
}
Nathan Schmidt
  • 374
  • 1
  • 4
  • 16
  • 1
    From where are you running the executable? – Fra93 Apr 21 '22 at 08:03
  • Why are you using `%d` to write addresses? – Fra93 Apr 21 '22 at 08:04
  • "I'd like fopen to default to the current directory when no path is given (this is what I'm used to and I think is the norm), but it's defaulting to my user folder". `fopen` has no idea what your user folder is, or what a folder is for that matter. It gets a file path and passes it to the OS. The OS knows that all relative paths go to the current directory. If they go to your user folder, then that's what your current directory is. – n. m. could be an AI Apr 21 '22 at 08:05
  • Can I have my current directory be wherever the executable is being run from? That's how it works when I compile the code in Windows. – Nathan Schmidt Apr 21 '22 at 08:14
  • @Fra93: Wherever the executable is being run from, it will treat my user folder as the current directory. I used `%d` to print the address to see if it was 0. – Nathan Schmidt Apr 21 '22 at 08:22
  • Where you executable is has nothing to do with your current directory. Unix is not Windows. – n. m. could be an AI Apr 21 '22 at 08:34
  • @n.1.8e9-where's-my-sharem. : Ok, I'm new to Unix coming from Windows. Can I have the executable open files from the same folder it's in and have that folder's location be variable? – Nathan Schmidt Apr 21 '22 at 08:43
  • There is no standard way for an executable to determine in which directory it resides. There may be some Apple-specific non-portable way but I am not aware of it. – n. m. could be an AI Apr 21 '22 at 08:57
  • Maybe [this](https://stackoverflow.com/questions/7511864/get-real-path-of-application-from-pid) is one such method. An executable can determine its own PID with `getpid()`. – n. m. could be an AI Apr 21 '22 at 09:03
  • 1
    You did not yet answer where and how you start your program. Do you call it from the command line? Do you double-click some link? -- The correct way to print pointers is by `printf("%p\n", (void*)fp);`. And you should guard the following code by a conditional to avoid the segmentation fault. – the busybee Apr 21 '22 at 09:29
  • 1
    Windows executables work the same way. – stark Apr 21 '22 at 11:30

0 Answers0