Opening a File with different text editors
Apparently this supposed to be possible. For example opening and operating on a file with NOTEPAD, or HxD. But aren't they all text files...how would one specify which text editor to open the file and operate on the file with using th开发者_开发百科e WINDOWS API. It is certainly not in "CreateFile".
Hopefully I'm understanding your question... The easiest way to do this is to launch the desired editor and pass the filename as an argument, rather than "invoking" the file (which will launch the default program associated with the file type).
For example, notepad.exe mytextfile.txt
or gvim.exe mytextfile.txt
.
If the editor is not on your %PATH%
, you'll need to use a full path file name.
What are you trying to do, exactly? You could:
- Maintain a list of editors that you expect to be installed and have entries for in the system's PATH (bad idea)
- Have an editor/editors that you want to use, query the Windows registry to find the installation path of the editors (using
RegGetValue
), and launch the editor withCreateProcess
) (a little better idea) - Query the registry to get the default editor for a given file type and then launch that editor using
CreateProcess
. (best idea)
But it all depends on what your goal is really.
Edit based on requirements
So, just so we're on the same page, from C++, you want to:
- Take a command line parameter to your C++ application (filename)
- Open that file in an arbitrary editor
- Detect when the user has made changes to that file
- Operate on the file contents
Is that correct?
If so, you could:
- Use Boost libs to compute a CRC for the current data in the file
- Launch an editor using one of the methods I initially described
- Stick in a tight loop and
sleep
so you don't chew up resources while the initially computed CRC matches one calculated every iteration of the loop
Of course, there are all kinds of issues that you'd have to deal with (that's just a super simple way of describing the algorithm I might use), such as:
- What happens if the user doesn't change the file?
- What happens if the file isn't found?
I'm sure that there are a number of different methods of doing this, but this is the easiest method that I can think of at the moment (while still being able to be fairly certain of the changes).
Disclaimer: I haven't implemented something like this, so I might be completely off base ;)
Are you looking for the ShellExecute()
or ShellExecuteEx()
APIs on Windows? They'll launch whatever program is registered for a file (generally based on the filename extention).
精彩评论