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