Home > front end >  Can't access files when running program via SSH
Can't access files when running program via SSH

Time:08-05

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 [email protected] /tmp/myprog

fopen() always returns null pointer.

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

ssh [email protected]
cd /tmp/
./myprog

fopen() works perfectly.

CodePudding user response:

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

  • Related