Making fopen() open files from a certain d

2019-02-28 02:19发布

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?)

标签: c shell
3条回答
叼着烟拽天下
2楼-- · 2019-02-28 02:46

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
查看更多
虎瘦雄心在
3楼-- · 2019-02-28 02:49

That opens a file from your current working directory.

You can change the current working directory using chdir.

查看更多
Summer. ? 凉城
4楼-- · 2019-02-28 02:50

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.

查看更多
登录 后发表回答