Launch txt file from console app..

Hey all,

I'm trying to launch a particular txt file from my console app..

So far i have tried:

system("Notepad.exe", FULL_TXT_PATH);

But pre-compiler its telling me FULL_TXT_PATH is undefined but this is the variable that contains the full path and filename that is declared only a few lines above this and even in the same function..

I tried:

system("Notepad.exe FULL_TXT_PATH");

..which launches notepad but windows dialog says FULL_TXT_PATH.txt does not exist.


I've looked at:
 
ShellExecute(hwnd, "open", "FULL_TXT_PATH", NULL, NULL, SW_SHOW);


...but this is not recognised. I do have #include <Shellapi.h> - is this the correct header?

Anyone point me in the best / right direction for launching a txt file please? :)

Paul..
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <cstdlib>

int main()
{
    const std::string FULL_TXT_PATH = "place the path to the file to be opened here" ;

    const std::string program = "C:\\windows\\system32\\notepad.exe" ; // favour using a fully qualified path
    const char quote = '\'' ; // required if the path contains space
    const std::string command = program + ' ' + quote + FULL_TXT_PATH + quote ;

    // drop elevated privileges if any
    std::system( command.c_str() ) ;
}
Hey JLBorges, thanks for replying :)

I tried your method and it opens notepad but states the file cannot be found.

FULL_TXT_PATH is declared globally and updated as this in the code:

FULL_TXT_PATH = ("MIDI_and_TXT_Files/" + FILE_NAME_TXT + ".txt");

...so the folder is relative to my application. Would this be the issues?
Last edited on
> Would this be the issues?

Check it out with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <iomanip>

bool file_exists( const std::string path ) { return std::ifstream(path).good() ; } 

int main()
{
    const std::string FULL_TXT_PATH = "place the path to the file to be opened here" ;

    if( file_exists(FULL_TXT_PATH) )
    {
        // ...
        // std::system
    }
    else std::cerr << "can't open file " << std::quoted(FULL_TXT_PATH) << '\n' ; 
}
Topic archived. No new replies allowed.