使用不同的文本编辑器打开文件

发布于 2024-11-01 08:50:59 字数 108 浏览 1 评论 0原文

显然这应该是可能的。例如,使用记事本或 HxD 打开和操作文件。但它们不都是文本文件吗...如何指定哪个文本编辑器打开文件并使用 WINDOWS API 操作文件。它肯定不在“CreateFile”中。

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 the WINDOWS API. It is certainly not in "CreateFile".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷路的信 2024-11-08 08:50:59

希望我理解你的问题...最简单的方法是启动所需的编辑器并将文件名作为参数传递,而不是“调用”文件(这将启动与文件类型关联的默认程序) 。

例如,notepad.exe mytextfile.txtgvim.exe mytextfile.txt

如果编辑器不在您的 %PATH% 上,您将需要使用完整路径文件名。

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.

装纯掩盖桑 2024-11-08 08:50:59

你到底想做什么?您可以:

  1. 维护您希望安装的编辑器列表,并在系统 PATH 中包含条目(坏主意)
  2. 拥有您想要使用的一个或多个编辑器,查询 Windows 注册表以查找编辑器的安装路径(使用 RegGetValue),并使用 CreateProcess 启动编辑器)(更好一点)
  3. 查询注册表以获取给定文件类型的默认编辑器,然后使用以下命令启动该编辑器创建进程。 (最好的主意)

但这一切都取决于您的真正目标是什么。

根据要求进行编辑

因此,我们在同一页面上,从 C++ 中,您希望:

  1. 将命令行参数传递给您的 C++ 应用程序(文件名)
  2. 在任意编辑器中打开该文件
  3. 检测当用户对该文件进行更改时
  4. 对文件内容进行操作

正确吗?

如果是这样,您可以:

  1. 使用 Boost 库来计算 CRC 用于文件中的当前数据
  2. 使用我最初描述的方法之一启动编辑器
  3. 坚持紧密循环并睡眠,这样在最初计算的 CRC 匹配时就不会消耗资源 如果

当然,您必须处理各种问题(这只是描述我可能使用的算法的一种超级简单的方式),例如:

  1. 用户不这样做会发生什么不改变文件?
  2. 如果找不到该文件会发生什么?

我确信有许多不同的方法可以做到这一点,但这是我目前能想到的最简单的方法(同时仍然能够相当确定更改)。

免责声明:我还没有实现类似的东西,所以我可能完全没有根据;)

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 ;)

听,心雨的声音 2024-11-08 08:50:59

您是否在寻找 ShellExecute()或 Windows 上的 ShellExecuteEx() API?他们将启动为文件注册的任何程序(通常基于文件名扩展名)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文