Not reading file from environment path - c++

I have create a project in which I am trying to generate a .txt file and then calling OS specific commands (I'm using unix so using system() function to trigger application) to open it in default application.
My code works perfect.
But the problem starts when I try to run my application after setting it in environment path. I am doing that for global visibility. File gets generated correctly by while opening the .txt using system() seems like it is trying to look at it at different directory.
I want to some how convey to the system() function to look at the specific location.
How can I solve this problem?

It sounds like you are actually just in a different working directory. Try showing the result of getcwd.

Related

Creating .lnk-file using only C++ and cmd

I have an .exe file and I want to add it to Autorun without using explorer. I know way to my .exe file. For adding something to Autorun I should create shortcut.lnk and put it to C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. I tried to use cmd command mklink, but this command creates .symlink, not .lnk.
How can I add my file to Autorun, apart from creating .lnk shortcut?
How create .lnk, using only C++ and cmd (not js, Powershell, BAT, etc.)
I know, that .lnk is just a txt file in HEX code? so I can put text with address in it and change add-in. It is not good way, in my opinion.
.lnk is a binary file with specific format. The proper way to enable autorun is to create a registry entry in Run or RunOnce sections.

Reading the command a program was called from

If this has already been answered elsewhere, I'm sorry, I couldn't find it.
I have an interesting problem whereby I have a compiled program, prog.exe, which reads a file prog.cfg which contains the config. If I open prog.exe by double-clicking on it, everything's good. However, if I open it from a command line or batch file, I first have to set the directory. For example, if I have my program in a progs folder on the desktop, and from the desktop run progs\prog.exe, it doesn't load the config because it's looking for the config on the desktop ie in the current working directory.
This is fine if you know about it, but it's just another hoop for users to jump through. Is there a way in c++ to backtrack to the command used to launch the program to deduce whether the config file will be in the current working directory or not?
Alternatively, am I asking completely the wrong question?
Many thanks!
You can use GetModuleFileName(nullptr, buf, bufsize) to get a path to the executable.
Note that the standard main function's argv[0] is not guaranteed to provide that path, and when it provides a path, is not guaranteed to provide a programmatically usable representation of the path.
if i understand you correctly, you always want your .exe read your .cfg file from the folder your .exe located? right?
try argv[0].
argv[0] is your full .exe path when you run your exe, and it is a parameter of main function.

How to use Instruments Time Profiler with Executable

I'm trying to use Mac Instruments Time Profiler to optimize my code for building a MandelBox. I found how to make my executable my target process, but when the program runs, it gives me an error in the Console window saying it cannot find the .txt file associated with the program.
Do I need to tell the profiler where to look to find the file? The text file is already in the same directory as the executable. Any thoughts? Thanks.
This problem is not unique to Instruments. The same thing would presumably happen if your current working directory was something other than the location of your program. For example, if you were to do cd / ; /path/to/yourprogram.
You either need to make your program find its own location and then find its text file as a sibling in the containing directory or take the path of the text file as an argument. Or, you will always have to set the working directory to your program's location before invoking it.
That last approach is an immediate workaround for the problem with Instruments. On the panel where you choose the target executable, you can also configure various parameters, such as arguments, environment variables, and the working directory. Set the working directory to the directory that contains the text file and it should work.

Matlab will not run my C++ .exe file

I have program in C++ that runs great but it has different steps in it. It opens a file first then does some calculations based on the data it gets from the file. Now I'm trying to run it through matlab. I tried mex but it got too complicated because I have VS 2010 Express and MATLAB 2007b. And so mex can never find my c compiler.
I'm now trying to just run it straight from the C++ program executable file. Here's how I tried to do it:
system('C:\path\file.exe')
it would run but the command window freezes and I have to close Matlab every time to get back on track. Then I tried to double click on the executable file it self. It flashes but I can see it outputs something from my file "file could not be uploaded" which is generated by a part of my program if it cant find the file.
So then I realized it's probably because of that that caused it to not run and crash.
So I wrote a sample code to open the file first before the system command open the .exe file. I've done something like fopen('fname','r'), but nothing works. Please note that this file I'm trying to open is a .COF file.
I tried running the .exe file using debug and release modes but nothing happens. It gives no errors which means it sees the .exe file but the command line doesn't come up and matland command window freezes.
After running, it's supposed to prompt the user at the command line then take input arguments and output something...
Please help....
I am guessing that your .COF file is in a different path than what's accessible by your binary. If you can recompile, try an absolute path name, and test it first without MATLAB.
There are more thorough ways to solve this, like passing the filename to your binary as a command line argument, or to read up what the rules are for the "current directory" when you use system, but perhaps you'd be happy with the quick solution.

Open file to display content in C++

I have 2 questions to ask regarding opening files (any kind of files) using C++. I am currently working on a GUI program and I want to add a changelog in txt form. Also I want a menu in my program to open that changelog.txt with the default text editor every user has installed or to simply put it to open that text file. Please keep in mind that I want to open the file for display NOT in the program for input/output.I know I can do that using
system("notepad.exe filepath.txt");
or to open them with the preset program:
system("filepath.txt");
The problem is that both of those open a command line behind the notepad. I know there is another command to open files using Win32 API called CreateProccess() but my compiler doesn't recognise that command (OpenWatcom W32).
So here are my questions:
1) Is there any other command to open files or is there a way to stop the command line from opening when using system command?
2) How do you define in Windows that the text file is in the current program folder? I mean instead of giving the entire filepath which will change from user to user is there any way to "tell" the program that the file is always on the current folder that the program is in?
I am sorry for any errors, if you want any clarification please let me know.
CreateProcess would be the wrong function to use here. That would require you to decide which process to run. The user may prefer to use a text editor other than Notepad, I know I do! The right way to do this on Windows is to ask the shell to open the file with whatever program the user has associated with the file. The ShellExecute function does this.
Call it like this:
ShellExecute(
MainWindowHandle,
"open",
FullyQualifiedTextFileName,
NULL,
NULL,
SW_SHOWNORMAL
);
You will need to include the Shellapi.h header file and link to the Shell32.lib library. If your compiler does not include these files, and I would be surprised if that was the case, then you can get them from the Platform SDK. That said, if you are serious about programming on Windows you should get hold of a tool that gives you access to the Windows API.
I do recommend that you use a fully qualified path for a task like this. Since your text file is located in the same directory as the executable you should simply join that directory to your text file's name. Get hold of the full path to the executable by calling GetModuleFileName passing NULL for the hModule parameter.