What is the correct way to use ShellExecute() in C to open a .txt

后端 未结 4 1927
时光说笑
时光说笑 2020-12-18 14:26

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

I would like to use ShellExecute(); to do this and i have done a lot of

4条回答
  •  醉梦人生
    2020-12-18 14:47

    This should work for you:

    #include 
    #include 
    
    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. 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.

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

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

提交回复
热议问题