Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't access files when running program via SSH

Here is part of my C program:

FILE* f;
f = fopen("data/file.bin","rb");

when program is running via SSH like this:
ssh -t root@xxx.xxx.xxx.xxx /tmp/myprog

fopen() always returns null pointer.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However when I’m connecting to the device via SSH and running myprog, like this:

ssh root@xxx.xxx.xxx.xxx
cd /tmp/
./myprog

fopen() works perfectly.

>Solution :

data/file.bin is a relative path, so the location it refers to depends on what directory you’re in (your "working directory") when you run the program.

If you cd to /tmp first, it’ll resolve to /tmp/data/file.bin. If you don’t, logging into root probably puts you someplace like /root, so it’ll resolve to /root/data/file.bin instead, and that presumably doesn’t exist.

(Note: a program can change its own directory with something like chdir(). But by default it just inherits the working directory of whatever ran it, which in this case is going to the shell that sshd launched.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading