0

I'm basically trying to launch a command prompt from my program. that command terminal is supposed to do whatever command it gets from my main program (I'm getting input with getline) and THEN output it to a file (out.txt). then I want to display the contents of the file in the main program's console.

the problem is that the command is not exactly doing anything to the file, although the command I forward seems to be the right one (for example, "dir > out.txt"). I thought that maybe it doesn't have permissions, but I opened a non-elevated command prompt in the source folder and it can easily do whatever I tell it to.

here's the source code, it's visual c++ under windows. I'm guessing I did something wrong with ShellExecute. thanks in advance.

#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    LPCSTR s;
    string command;
    string line;
    ifstream fd("out.txt");

    cout << "enter a command:\n";
    getline(cin, command);

    command += " > out.txt";

    cout << "command: " << command << endl;

    s = (LPCSTR)command.c_str();

    ShellExecute (NULL, "open", "cmd", s, " C:\\ ", SW_SHOW);

    cout << "s: " << s << endl;

    if(fd.is_open())
    {
        while(getline(fd, line))
            cout << line << endl;
        fd.close();
    }

    else cout << "unable to open file lel" << endl;

    _getch();
    return 0;
}
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
icarus
  • 166
  • 1
  • 6
  • 14

0 Answers0