AKO
AKO

Reputation: 172

Making fopen() open files from a certain directory

I have a function with something like

FILE *file1 = fopen("testing.txt", "r");

I can't modify this line. However, if I make a file named "testing.txt" in, say /tmp, would I be able to make the function load the file from /tmp instead of it's own directory. (Maybe by modifying the PATH variable?)

Upvotes: 1

Views: 540

Answers (3)

Box Box Box Box
Box Box Box Box

Reputation: 5240

See this.

This is using C code.

You can also use cd.

For example, go to the terminal:

$ cd /tmp
$ cd /path_to_your_program

Also, cd .. will make you go to the directory above, and cd will make you go to the home directory.

Also, if you do not have the program in the directory in which you have to compile it, you can use cp which copies file.

$ cp /path_to_copy_from /path_to_copy_to

Then you can go to that directory, and run it from there.

I would recommend you to take a basic linux tutorial like this.

Upvotes: 0

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137418

If the program doesn't change its own working directory, you could cd into /tmp and simply run the program from there.

$ cd /tmp
$ /absolute/path/to/my_program

Upvotes: 5

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136266

That opens a file from your current working directory.

You can change the current working directory using chdir.

Upvotes: 1

Related Questions