Get the original location where the program is called [duplicate] - c

This question already has answers here:
How do I get the directory that a program is running from?
(26 answers)
Closed 6 years ago.
Is there any way to know the directory where the program is originally spawned in C? I want my program to know where it is located in the computer. I already tried using _getcwd() in direct.h. I also tried getcwd() using unistd.h. But here's the problem. If I added a PATH to my program's directory, the functions _getcwd() and getcwd() path will return the path where I called the program. So if I ran the program in dekstop, it will return the path of the desktop instead. I already tried using this method but it doesn't fix the problem. It returns the value of the calling path. I would like to know what function to use to know the path of the program and not the path that the program is called.
What function should I use?

The current working directory is of no use whatsoever. There is no connection between an executable image's location in the filesystem and the current working directory, other than coincidental.
You can retrieve the fully qualified path to the executable by calling _get_wpgmptr (a Microsoft-specific extension to the CRT that ships with Visual Studio) or GetModuleFileName (passing NULL for hModule), and extract the path by calling PathRemoveFileSpec (or PathCchRemoveFileSpec for Windows 8 and above).

Related

Is there a simple way to check if a file exists in C (Visual Studio)?

I know this is a frequently asked question, but most solutions I have found are from 6-10 years ago and don't seem to work.
As a part of the C program I am writing in Visual Studio, I need to find a function that is able to return a boolean value - whether or not a file with a given name exists (the file in question is located in the debug directory, which is why I am saying file name and not file path).
I need to implement it using a library I am able to include in VS, hence using access() from the unistd.h library will not work.
Also, it has to be a safe function.
If there's a function in the WINAPI that does all of that - that would be best.
Thanks in advance for the help.
PathFileExistsW should do the job. It takes the path of the file or directory, which you want to check the existence of, as the first argument. It returns BOOL (TRUE, if the file or directory exists, and FALSE, if it doesn't. You have to include shlwapi.h as header and link against Shlwapi.lib in order to use this function.

Matlab / M - pass a cell array into a function as input [duplicate]

This question already has answers here:
"Undefined function 'function_name' for input arguments of type 'double'."
(3 answers)
Closed 5 years ago.
I'm a new user of Matlab, can you please help:
I have the following code in an .M file:
function f = divrat(w, C)
S=sqrt(diag(diag(C)));
s=diag(S);
f=sqrt(w'*C*w)/(w'*s);
I have stored this file (divrat.M) in the normal Matlab path, and therefore I'm assuming that Matlab will read the function when it's starting and that this function therefore should be available to use.
However, when I type
>> divrat(w, C)
I get the following error
??? Undefined function or method 'divrat' for input arguments of type 'double'.
What is the error message telling me to do, I can't see any error in the code or the function call?
You get this error when the function isn't on the MATLAB path or in pwd.
First, make sure that you are able to find the function using:
>> which divrat
c:\work\divrat\divrat.m
If it returns:
>> which divrat
'divrat' not found.
It is not on the MATLAB path or in PWD.
Second, make sure that the directory that contains divrat is on the MATLAB path using the PATH command. It may be that a directory that you thought was on the path isn't actually on the path.
Finally, make sure you aren't using a "private" directory. If divrat is in a directory named private, it will be accessible by functions in the parent directory, but not from the MATLAB command line:
>> foo
ans =
1
>> divrat(1,1)
??? Undefined function or method 'divrat' for input arguments of type 'double'.
>> which -all divrat
c:\work\divrat\private\divrat.m % Private to divrat
As others have pointed out, this is very probably a problem with the path of the function file not being in Matlab's 'path'.
One easy way to verify this is to open your function in the Editor and press the F5 key. This would make the Editor try to run the file, and in case the file is not in path, it will prompt you with a message box. Choose Add to Path in that, and you must be fine to go.
One side note: at the end of the above process, Matlab command window will give an error saying arguments missing: obviously, we didn't provide any arguments when we tried to run from the editor. But from now on you can use the function from the command line giving the correct arguments.
The most common cause of this problem is that Matlab cannot find the file on it's search path. Basically, Matlab looks for files in:
The current directory (pwd);
Directly in a directory on the path (to see the path, type path at the command line)
In a directory named #(whatever the class of the first argument is) that is in any directory above.
As someone else suggested, you can use the command which, but that is often unhelpful in this case - it tells you Matlab can't find the file, which you knew already.
So the first thing to do is make sure the file is locatable on the path.
Next thing to do is make sure that the file that matlab is finding (use which) requires the same type as the first argument you are actually passing. I.el, If w is supposed to be different class, and there is a divrat function there, but w is actually empty, [], so matlab is looking for Double/divrat, when there is only a #(yourclass)/divrat. This is just speculation on my part, but this often bites me.
The function itself is valid matlab-code. The problem must be something else.
Try calling the function from within the directory it is located or add that directory to your searchpath using addpath('pathname').
The error code indicates the function definition cannot be found. Make sure you're calling the function from the same workspace as the divrat.m file is stored. And make sure divrat function is not a subfunction, it should be first function declaration in the file. You can also try to call the function from the same divrat.m file in order to see if the problem is with workspace selection or the function.
By the way, why didn't you simply say
s = sqrt(diag(C));
Wouldn't it be the same?
Also, name it divrat.m, not divrat.M. This shouldn't matter on most OSes, but who knows...
You can also test whether matlab can find a function by using the which command, i.e.
which divrat
I am pretty sure that the reason why this problem happened is because of the license of the toolbox (package) in which this function belongs in. Write which divrat and see what will be the result. If it returns path of the function and the comment Has no license available, then the problem is related to the license. That means, license of the package is not set correctly. Mostly it happens if the package (toolbox) of this function is added later, i.e., after installation of the original matlab. Please check and solve the license issue, then it will work fine.

fatal error: sndfile.h.in: No such file or directory [duplicate]

This question already has answers here:
What mean file with extension "h.in"?
(3 answers)
Closed 8 years ago.
I was compiling with gcc on Linux
Because sndfile.h was not there but sndfile.h.in was found, I just tried with sndfile.h.in - which is in the same directory as the *.C file.
But I got the error even though it is in the same directory. Its been a while since I programmed in Linux that these little things are bothering me - appreciate if u could help me started. Thanks
I think you are using the angular brackets for the including the file.If you place < >. It will search in /usr/include. You have to use the double quotes for including the file in the current directory. And be sure that file is available.
Like this.
#include "sndfile.h.in"

Passing a variable to chdir() in C (Linux) [duplicate]

This question already has answers here:
Changing working directories in Linux shell in a C program
(2 answers)
Closed 7 years ago.
I'm having a problem with chdir() in my C program - only when running on Linux (works fine on Mac). I've stripped down my code.
Something like this works fine:
chdir("/Documents");
but when I try to pass it as a variable it doesn't want to work.
char *home_directory;
home_directory = malloc(80);
chdir(home_directory);
Home directory is read from a file elsewhere in my code, I can post how I've done that if needed.
Thanks.
Since you are reading home_directory from a file, have you forgotten to remove a trailing newline and any other spurious characters before you use it as a parameter to chdir?
Try doing some error checking, like this;
char *home_directory;
home_directory = malloc(80);
/* ...fill the home directory... */
if (chdir(home_directory) == -1)
perror("chdir");
.. and then with a man chdir yo can look up the meaning of the error code.
Since chdir is a system call on Linux, you can simply use the strace program to see what your program really does. That doesn't free you from handling errors preoperly in your code; obviously. It just helps you to follow the actions that your program takes.

How can a currently running C program find out what directory it is located in?

Say I have a command line C program which is currently executing, and I want to read a file or execute another binary in the same directory - how can I find out what directory that is?
Note that I'm not looking for the current working directory. The user may have invoked my original program in any of the following ways (and possibly others I don't know about).
../../program
/home/matt/program
PATH=$PATH:/home/matt program
Ideally I'm looking for something which will work on a unix system and windows via MinGW.
http://c-faq.com/osdep/exepath.html
According to the C FAQ it can't be done reliably
Finding current executable's path without /proc/self/exe
Concat getcwd() and dirname(argv[0])

Resources