Pass a relative path to fopen()

71,368

Solution 1

in = fopen("path", " r ");

Two wrongs right there:

  1. You don't want to open the file literally named "path", you want the file whose name is in the variable path
  2. the mode argument is invalid; you want "r"

To get them right do

in = fopen(path, "r");

Solution 2

First, you need to add some space to path in order to fit the content of array[0] in strcat, otherwise you'll be writing past the allocated area.

Second, you are not passing path to fopen, because you enclosed "path" in double quotes.

char path[100] = "./textfiles/"; // Added some space for array[0]
strcat( path, array[0] );

// Open the file
FILE *in;
in = fopen( path, " r " ); // removed quotes around "path"
if (!in) 
{
    printf("Failed to open text file\n");
    exit(1);
}

Solution 3

path is only large enough to contain the characters it has been initialised with. The size of path must be increased. Allocate memory for path based on the filename being appended:

const char* dir = "./textfiles/";
const size_t path_size = strlen(dir) + strlen(array[0]) + 1;
char* path = malloc(path_size);
if (path)
{
    snprintf(path, path_size, "%s%s", dir, array[0]);

    in = fopen(path, "r"): /* Slight differences to your invocation. */

    free(path);
}

Solution 4

Your problem has nothing to do with fopen and everything with C string handling: strcat requires enough memory in the destination buffer for the entire string, but you only provide enough memory for the initial string. Bad! Using tools like valgrind would tell you about your illegal access immediately, by the way.

Here's a naive solution:

char buf[1000] = { };
strcat(buf, "./textfiles/");
strcat(buf, array[0]);

// ...
Share:
71,368
Dino55
Author by

Dino55

Updated on July 16, 2022

Comments

  • Dino55
    Dino55 almost 2 years

    I am trying to pass a relative path to fopen(), but it can't seem to find the file. I need this to work on Linux. The filenames (ex: t1.txt) are saved in an array. So all I need is the "front part" of a relative path.

    Here's my code:

    // Use strcat to create a relative file path
    char path[] = "./textfiles/"; // The "front part" of a relative path
    strcat( path, array[0] ); // array[0] = t1.txt
    
    // Open the file
    FILE *in;
    in = fopen( "path", " r " );
    if (!in1) 
    {
        printf("Failed to open text file\n");
        exit(1);
    }
    
  • Dino55
    Dino55 about 12 years
    i didn't think of that at all. Thanks for the reply!
  • Dino55
    Dino55 about 12 years
    oh wow, haha! would never caught the first error by myself. And yea, I like to add spaces so the code doesn't look crowded. Guess it's a bad idea! Anyways, Thank you!
  • pmg
    pmg about 12 years
    Don't forget the suggestions in the other answers, specifically the size of the array path and don't test for in1 when your FILE * is in.
  • markgz
    markgz about 12 years
    I think you meant to say strcat( path, argv[1] ), since argv[0] is the name of the current program.
  • Sergey Kalinichenko
    Sergey Kalinichenko about 12 years
    @markgz Thanks! Actually, I meant to say array[0] to follow the OP's code, but intellisense put argv, and I did not notice. This is now fixed.