在 C 中使用 ShellExecute() 打开 .txt 的正确方法是什么 [英] What is the correct way to use ShellExecute() in C to open a .txt

查看:24
本文介绍了在 C 中使用 ShellExecute() 打开 .txt 的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我需要打开一个 .txt 文件,该文件将在与程序相同的文件中创建.

Alright so i need to open a .txt file that will be created in the same file as the program.

我想使用 ShellExecute();要做到这一点,我已经对它进行了大量研究,但我似乎无法获得正确的语法,主要是因为我不知道如何处理参数HWND"

I would like to use ShellExecute(); to do this and i have done a lot of research on it and i just cant seem to get the syntax correct mostly because i dont know what to do with the parameter "HWND"

我查看了这里 获取答案并获得所有信息,除了要放入 HWND 的内容

I looked here for the answers and got all the info except what to put in HWND

这是我需要使用的代码的方式:

Here is how i need the code used:

ShellExecute(0,"open","c:\debug.txt",NULL,NULL,1);

感谢您的帮助,如果您不确定我在说什么!:)

Thanks advance for the help ask if you are unsure what i am talking about! :)

这是我用来测试功能的程序:

This is the program that i use to test the function:

  #include "DAL.h"
//DAL.h added to Testing file to make compiling easier
//Created to test show_debug()
int main(void)
{
  int test1,test2,final;

  puts("Enter 2 numbers to add (2,2)");
  scanf("%d,%d",&test1,&test2);

  log_debug(test1);
  log_debug(test2);

  view_debug();

  final= test1+test2;
  printf("%d
",final);

  log_debug(final);

  return(0);
}

view_debug();是包含 ShellExecute 的函数

view_debug(); is the function that includes ShellExecute

void view_debug(void)//WIP
//Opens the debug.txt in notepad
{
    LoadLibrary( "shell32.dll" );
    ShellExecute(0,"open","c:\debug.txt",NULL,NULL,1);
}

这是 log_debug();

This is log_debug();

int log_debug(int test_variable)
//This function simply tests the programmers desired veriable & displays it for help in keeping track of a veriables value(integer).
//The function has support for upto 1 variable for testing
{
    time_t now;
    time(&now);

    FILE *debug; //Creates file to write debug info

    debug=fopen("debug.txt", "a+");
    fprintf(debug,"DEBUG %.24s: <%d>
", ctime(&now),test_variable);
    //TODO: Allow more than one variable

    fclose(debug);

    return(0);
}

文件由函数log_debug()创建;它确实有效,但必须手动打开,因为 ShellExecute 无效.

The file is created by the function log_debug(); and it does work but must be opened manually because ShellExecute does not work.

完整来源此处.

推荐答案

这应该适合你:

#include <windows.h>
#include <ShellApi.h>

void view_debug(const char* pszFileName)
{
    ShellExecuteA(GetDesktopWindow(),"open",pszFileName,NULL,NULL,SW_SHOW);
}

int main()
{
    view_debug("c:\debug.txt");
}

如果它不起作用,那么可能有两三个原因:

If it doesn't work, then there are likely two or three reasons:

  1. 你用你的程序代码创建了 debug.txt,但文件仍然被锁定,因为你没有关闭文件句柄(例如,取决于你用 log_debug 打开文件的方式:fclose(), CloseHandle()、close() 等...) 或者因为您在没有 FILE_SHARE_READ 标志的情况下打开了文件.

  1. You created the debug.txt with your program code, but the file is still locked because you didn't close the file handle (e.g. depending on how you opened the file with log_debug: fclose(), CloseHandle(), close(), etc...) or because you opened the file without the FILE_SHARE_READ flag.

您实际上没有从 c: 驱动器的根目录读取的权限.对于非管理员帐户,这通常是正确的.

You don't actually have permissions to read from the root of the c: drive. Which is usually true for non-admin accounts.

c:debug.txt 实际上并不像你想象的那样存在.

c:debug.txt doesn't actually exist like you think it does.

这篇关于在 C 中使用 ShellExecute() 打开 .txt 的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆