C programming - how to open a file in the current directory?

xcgames

Gawd
Joined
Oct 9, 2004
Messages
660
I'm currently using this to open a file that I wish to read, but how do you read a file from the current directory that the exe is in, without having to specify a path?

PHP:
fopen_s( &streamSorted, "c:/sort/DataAfterSorting.txt", "r" )
 
Use the "."

So say I'm in /home/username/, and there's a file called "report.txt," and I have a script uses report.txt for whatever reason and it's in the same directory.

You reference report.txt as...

Code:
./report.txt

".." is used for going up one directory.
 
The dot will work, but you can also just use the file name directly:

Code:
fopen_s( &streamSorted, "DataAfterSorting.txt", "r" )

is fine, too.
 
This might be my ignorance of C, but in C#, you can't rely on the fact that the executable is in the current working directory.

In C#, if you're using OpenFileDialog's or SaveFileDialog's (for example) it will change the current working directory, forcing you to use System.Windows.Forms.Application.StartupPath or System.Reflection.Assembly.GetExecutingAssembly().Location

Does C not have that requirement? Will "." always resolve to the directory the executable lies in?
 
Jason Isom said:
Does C not have that requirement? Will "." always resolve to the directory the executable lies in?

Unless chdir(2) was issued.
 
C (and C++) both don't know about OpenFileDialog(). Anything, anywhere can change the current working directory. When you call something like OFD(), you're calling a library function that may, at its discretion, change the current directory.

"I want to use the current directory" is a different question than "I want to use the same directory as the location of the program's executable".

doh said:
Unless chdir(2) was issued.
This isn't true on Windows. If FOO.EXE is in C:\FOO, and I've got the shell open on D:\BAR, issuing the command "C:\FOO\FOO.EXE" will runn FOO.EXE with the current working directory of D:\BAR.

Similarly, the current working directory for programs run from the Windows Shell (the Explorer) is set by the "Start in" edit field in the shortcut you've used.
 
mikeblas said:
"I want to use the current directory" is a different question than "I want to use the same directory as the location of the program's executable".

I suppose the original poster will have to provide information that isn't conflicting, because he states: "I'm currently using this to open a file that I wish to read, but how do you read a file from the current directory that the exe is in, without having to specify a path?" and "C programming - how to open a file in the current directory?"
 
If you want to generate a path to a file in the directory the exe is in and want it to work no matter where the exe is called from:

Code:
#include <cstdio>
#include <string>
using namespace std;

// Use argv[0] to generate an absolute (if necessary) path to a file in the directory of this exe
// Works on *nix also
string generatePath(const string& s, const string& file) {
    if (s.find("/") != string::npos) {
        return s.substr(0, s.rfind( "/" )) + "/" + file;
    } else if (s.find("\\") != string::npos) {
        return s.substr(0, s.rfind("\\")) + "\\" + file;
    } else {
        return file;
    }
}

int main(int, char* argv[]) {
    const string file = generatePath(argv[0], "file.txt");
    printf("%s\n", file.c_str());
}

// g++ -Wall -Wextra -ansi -pedantic this.cpp -o this -O3 -s

Mike, that should look familiar as you and LoS helped me with that before.

Mind making a char array version of generatePath for C use? I'd probably leak, but if you require effort, I'll do so.

Edit:

Here's the effort anyway

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void subcpy(char* target, const char* const src, const char* const stop) {
    size_t i = 0;
    while(&src[i] != stop) {
        target[i] = src[i];
        ++i;
    }
    target[i] = 0;
}

char* generatePath(const char* const arg0, const char* const file) {
    char* p = (char*)malloc(sizeof(char) * (strlen(arg0) + strlen(file) + 2 ));
    if (!p) {
        exit(1);
    }
    if (strchr(arg0, '/')) {
        subcpy(p, arg0, strrchr(arg0, '/') + 1);
        strcat(p, file);
    } else if (strchr(arg0, '\\')) {
        subcpy(p, arg0, strrchr(arg0, '\\') + 1);
        strcat(p, file);
    } else {
        strcpy(p, file);
    }
    return p;
}

int main(int argc, char* argv[]) {
    char* path;
    if (!argc) { /* get rid of unused argc warning */
        return 1;
    }
    path = generatePath(argv[0], "file.txt");
    printf("%s\n", path);
    free(path);
    return 0;
}

/* gcc - Wall -Wextra -ansi -pedantic this.c -o this -O3 -s */
 
Back
Top