How do I open a file in c++ (other than notepad)

10,789

Solution 1

I think you're confused.

System executes a command as you would on the command line (type cmd into the run prompt under start menu to get that).

So, when you type notepad.exe test.txt it's saying:

Open the program notepad.exe which is on the system path (so the command line can find it to execute that program), and pass the parameter test.txt to it.

Notepad itself decides what to do with test.txt, in this case it opens it.

So, you can tell it to run any command (program/executable) and pass any parameters to it in reality. If excel's on your system path, you can probably just type excel.exe to open it from your system command. Otherwise, find the location excel is installed in, and refer to it with the whole path to excel.exe and it will work fine.

For example, on my computer, executing "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" would open excel from the command line. I can pass further parameters to it by having more information (like filenames) after the Excel.exe" portion, just as you did in your notepad example. Using your system command should have equivilent behavior when that line is executed.

Solution 2

If you are only targeting Windows systems you can use the ShellExecuteEx function (part of the Win32 API). You can just pass a filename to it and it will launch what ever program is registered to handle that file type (just as if you opened the file from windows explorer). Documentation is available on MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx

There is some examples on Launching Applications (ShellExecute, ShellExecuteEx, SHELLEXECUTEINFO) MSDN article and lots more elsewhere around the internet.

Solution 3

AS the other guys mentioned , the System function only executes a cmd command, .. notepad.exe is in the system's path by default so it works directly but for example for me if I want to open a zip file on my desktop , I'd type something like

"C:\Program Files\7-Zip\7zFM.exe" Desktop\zipfile.zip

that's when I'm currently at the my user's directory [by default] , or

"C:\Program Files\7-Zip\7zFM.exe" C:\Users\JiMMaR\Desktop\zipfile.zip

[where JiMMaR is my user name on windows 7] note that this certain command works only on windows , if you are using another OS this won't work as it is

try doing a

fileNeededtoBeOpened = "\"C:\Program Files\7-Zip\7zFM.exe\" C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";

and see if that executes or not

edit: if you cannot escape the space , then try this one

fileNeededtoBeOpened = "C:\Program~1\7-Zip\7zFM.exe C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";
Share:
10,789
Omonogo
Author by

Omonogo

Updated on June 04, 2022

Comments

  • Omonogo
    Omonogo almost 2 years

    I was wondering how to open a file other than notepad... Our prof gave us an example:

    s = "notepad.exe test.txt";
    system(s.c_str());  
    

    That will open a file type of "notepad.exe" and the file name of "test.txt"

    Main Question:

    Now, I was wondering if there was a way to open other type of files, such as Microsoft Excel, Microsoft Word, Visual Studio, or 7zip.

    My attempt opened something in a new cmd.exe (because of the START keyword):

    fileNeededtoBeOpened = "START \"New Microsoft Office Excel Worksheet.xlsx\"";
    system(fileNeededtoBeOpened.c_str());   
    

    (This code is slightly different from my original, where I'm trying to open a file from a vector...) but all I really need to know is instead of "notepad.exe" or "START" is there a different command to open different file types that aren't .txt

    Also, a side note, I was reading on the internet that it wasn't safe to use system() to open files, is this correct?

    I found the answer by myself... for those who are curious, here an the answers:

    • To open a text file: system(notepad)
    • To open an excel file: system(start excel)
    • To open a word doc file: system(start winword)
    • To open a 7z file: system(start 7zFM)
    • To open a visual studio file: system(start devenv)