开发者

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:

  1. Maintain a list of editors that you expect to be installed and have entries for in the system's PATH (bad idea)
  2. 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 with CreateProcess) (a little better idea)
  3. 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:

  1. Take a command line parameter to your C++ application (filename)
  2. Open that file in an arbitrary editor
  3. Detect when the user has made changes to that file
  4. Operate on the file contents

Is that correct?

If so, you could:

  1. Use Boost libs to compute a CRC for the current data in the file
  2. Launch an editor using one of the methods I initially described
  3. 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:

  1. What happens if the user doesn't change the file?
  2. 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜