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 realy want to :|) I want to make a simple Texteditor where the user can paste a filepath and then edit the file directly in the console. (for learning purposes) i simply get the string throught std::cin and then throw it in system() for the directory change throught "cd". Well thats not working because for no reason system() needs a const char pointer as the argument. after converting the string throught ".data()" and pasting the pointer in the system() funktion it wont change the directory AND doesn´t throw a error or crashes
`
#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 so i appreciate the help :)
Well i fucked up with the string. thx guys.
A more serious problem is that the whole purpose of my code is nonesense 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 didnt knew that the console and the programm 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() :|
CodePudding user response:
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;
}