Błędy w funkcji, która powinna działać

0

Witam.
Coś w tej funkcji powoduje 17 błędów. Co to takiego może być?
Pozdrawiam

string pwshellWithReturnment(string command) {

	string returnment = "";

	string tempstr = to_string((rand() % 99999999999) + 7);

	//https://stackoverflow.com/questions/62469810/get-powershell-script-output-from-c-app
	string psfilename = "%temp%\\a\\"; psfilename.append(tempstr.c_str());
	string resfilename = "%temp%\\b\\"; resfilename.append(tempstr.c_str());

	std::ofstream psfile;
    psfile.open(psfilename);

	//redirect the output of powershell into a text file
	std::string powershell = "ls > " + resfilename + "\n";
	psfile << powershell << std::endl;
	psfile.close();

	system(command.c_str());
	//"start": run in background
	system((std::string("powershell.exe ") + psfilename).c_str());

	remove(psfilename.c_str());

	//after the exe finished, read the result from that txt file
	std::ifstream resfile(resfilename);
	std::string line;
	if (resfile.is_open()) {
		std::cout << "result file opened" << std::endl;
		while (getline(resfile, line)) {
			std::cout << line << std::endl;
		}
		resfile.close();
		remove(resfilename.c_str());
	}

	return returnment;

}
0

Oto błędy
screenshot-20220125233221.png

2

@Emski: najwazniejszy jest pierwszy error, ktory na swoim zdjeciu zamazales (reszta moze wynikac wlasnie z niego). Nie musisz nam wstawiac zdjecia, ale musisz przekopiowac jego tresc, inaczej nic z tego nie bedzie

2

Brakuje ci pliku nagłówkowego <fstream>. Dołącz go i powinno być OK.

Ten kod na ideone.com się kompiluje:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

string pwshellWithReturnment(string command) {

    string returnment = "";

    string tempstr = to_string((rand() % 99999999999) + 7);

    //https://stackoverflow.com/questions/62469810/get-powershell-script-output-from-c-app
    string psfilename = "%temp%\\a\\"; psfilename.append(tempstr.c_str());
    string resfilename = "%temp%\\b\\"; resfilename.append(tempstr.c_str());

    std::ofstream psfile;
    psfile.open(psfilename);

    //redirect the output of powershell into a text file
    std::string powershell = "ls > " + resfilename + "\n";
    psfile << powershell << std::endl;
    psfile.close();Ide

    system(command.c_str());
    //"start": run in background
    system((std::string("powershell.exe ") + psfilename).c_str());

    remove(psfilename.c_str());

    //after the exe finished, read the result from that txt file
    std::ifstream resfile(resfilename);
    std::string line;
    if (resfile.is_open()) {
        std::cout << "result file opened" << std::endl;
        while (getline(resfile, line)) {
            std::cout << line << std::endl;
        }
        resfile.close();
        remove(resfilename.c_str());
    }

    return returnment;

}

int main() {
	// your code goes here
	return 0;
}
0

Faktycznie, mój błąd. Byłem pewnien, że dodałem fstream. Dziękuję za pomoc

1 użytkowników online, w tym zalogowanych: 0, gości: 1