c++stringpointerstext-editorconst-char

Using a string in system() (maybe system_() )


My problem would be using a string in system().

As you might know, you can use console commands in C++ with system() (or system_() if you really want to). I want to make a simple text editor, where the user can paste a file path and then edit the file directly in the console (for learning purposes). I simply get the string through std::cin and then throw it in system() for the directory change through "cd".

Well, that's not working, because for no reason system() needs a const char* pointer as the argument. After converting the string through .data() and pasting the pointer in the system() function, it won't change the directory AND doesn't throw an error or crash.

#pragma once

#include <Windows.h>
#include <fstream>
#include <iostream>
#include <ostream>
#include <istream>
#include <string>

using std::fstream;

using namespace std;

int start_doin_da_stream(string h, string filename) {

    //now the parsing of the content into the cmd-shell shall beginn

    system("color 1b");
    string file = h;

    //changing directory
    string doc = file + '/' + filename;
    doc = "cd " + file;


    //maybe the issue
    char const* foo = doc.data();
    //

    system(foo);
    system("dir");

    //creating a file stream
    fstream stream(filename, std::ios::out | std::ios::app);

    //checking for living stream
    bool alive = true;
    if (alive != stream.good()) {
        std::cout << "my men... your file deaddd!!!";
        return 0;
    }
    else
    {
        std::cout << "Its alive yeahhhhhh!!!!!";
        std::this_thread::sleep_for(std::chrono::milliseconds(100000));
    }

    //if alive true gehts weiter ans schreiben in die Konsole

    return 0;
}

I don´t really know what else I could try, because I am relatively new to programming.


Well, I messed up with the string. thx guys.

A more serious problem is that the whole purpose of my code is nonsense that I understood after reading G.M.'s comment about mother and child processes. My understanding of C++ console applications was seriously lacking, as I didn't know that the console and the program are 2 different threads. Thanks G.M. for your knowledge. I'll try to get a workaround. There might be a solution to my problem already.


xD it was one damn function. the name is... hold yourself... SetCurrentDirectory().


Solution

  • You might have to transform your std::string into a c_string via the function c_str() (pay attention to single/double quotes):

    string doc = file + "/" + filename;
    doc = "cd " + file;
    
    system(doc.c_str());
    

    Also, checking the return value of system() might help you. It should return a 0 value if everything is correct. So you can just do this:

    string doc = file + "/" + filename;
    doc = "cd " + file;
    
    if(system(doc.c_str()))
        std::cout << "ERROR\n";
    

    [UPDATE]

    Since the provided code is a bit weird, this could be a more concrete solution:

    int start_doin_da_stream(string path, string filename) {
    
        //now the parsing of the content into the cmd-shell shall beginn
        system("color 1b");
    
        //changing directory
        string file_path = "cd " + path;
        system(file_path.c_str());
    
        //creating a file stream
        fstream stream(filename, std::ios::out | std::ios::app);
    
        //checking for living stream
        bool alive = true;
        if (alive != stream.good()) {
            std::cout << "my men... your file deaddd!!!";
            return 1; // you might want something different from 0 in order to debug the error
        }
        else // this else is not wrong but avoidable since the true condition has a return statement
        {
            std::cout << "Its alive yeahhhhhh!!!!!";
            std::this_thread::sleep_for(std::chrono::milliseconds(100000));
        }
    
        //if alive true ghets weiter ans schreiben in die Konsole
    
        return 0;
    }