Reputation: 145
I am trying to show user a text file after an operation completed in a c program using winapi but I want the file to be opened using notepad (or the default text processor application). how do I do this (opening a text file using window's default application) using winapi?
Upvotes: 0
Views: 7771
Reputation: 10841
Given a file test.txt
, you can display it in notepad
on Windows with:
#include <stdlib.h>
int main () {
system ("notepad test.txt");
return 0;
}
If you can be sure that your program will default to the cmd
shell, you can just say system ("test.txt")
and the file will open in whichever text editor is set as the default, but specifying the editor is safer because it deals with cases like the program being compiled under Cygwin, where the default shell will be sh
, which doesn't open text files by default when you supply the filename at the command prompt.
If you can't be sure your program will always be compiled on Windows then I would favour the ShellExecute()
solution outlined by @Remy Lebeau. A program containing #include <shellapi.h>
and a call to ShellExecute()
will fail to compile on a non-Windows system (thereby pre-empting an error) whereas my solution will compile on a non-Windows system but will fail during runtime at the system()
call.
Upvotes: 3
Reputation: 595782
ShellExecute()
is what you are looking for, eg:
#include <shellapi.h>
ShellExecute(NULL, NULL, "C:\\path\\to\\myfile.txt", NULL, NULL, SW_SHOWNORMAL);
Upvotes: 7