Jump to content

Guardar o output do comando system("set userprofile") numa string


revised

Recommended Posts

Boa tarde.

Gostaria de uma pequena ajuda aqui neste problema.

O objectivo é guardar o output do comando system("set userprofile") numa string.

O código que tenho para já é este.

#include<iostream>
#include<Windows.h>
#include<tchar.h>

using namespace std;

void main(){
   _tsetlocale(LC_ALL, _T("portuguese"));
   char caminho[100] = system("set userprofile");
   cout << "Caminho: " << caminho;
   system("pause");
}

Já fiz varias pesquisas no entanto não consigo encontrar o método correto.

Obrigado

Edited by pwseo
syntax highlight
Link to comment
Share on other sites

Bem apesar da ajuda aqui prestada consegui resolver o problema.

Vou colocar o codigo pois pode dar jeito a alaguem...

#include <windows.h>
#include <iostream>
#include <shlobj.h>
#include <string>
#include <tchar.h>
#include <fstream>
#include <stdio.h>

using namespace std;

string setPathVB(string vBoxPath){
   vBoxPath = vBoxPath + "\\.VirtualBox\\VirtualBox.xml";
   return vBoxPath;
}

string getPathVB(){
   char aux[MAX_PATH];
   string vBoxPath;
   SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, aux);
   vBoxPath += aux;
   vBoxPath = setPathVB(vBoxPath);
   return vBoxPath;
}

string setVirtMachinPath(string vMachinesPath){
   vMachinesPath = vMachinesPath + ":\\VirtualBox VMs\\";
   return vMachinesPath;
}

string getVirtMachinPath(){
   char op;
   string vMachinesPath;
   do{
       system("cls");
       cout << "Unidade do disco externo: "; cin >> op;
       if ((op < 'a' || op > 'z') && (op < 'A' || op > 'Z')){
           system("cls");
           cout << "\n\n\n\n\n\n\n\n\n\n\n\n\t\t\tSó pode inserir letras de \"A\" a \"Z\"";
           fflush(stdin);
           getchar();
       }else{
           vMachinesPath += op;
           vMachinesPath = setVirtMachinPath(vMachinesPath);
       }
       system("cls");
   } while ((op < 'a' || op > 'z') && (op < 'A' || op > 'Z'));    
   return vMachinesPath;
}

void main(){
   _tsetlocale(LC_ALL, _T("portuguese"));

   string vBoxPath = getPathVB();
   string vMachinesPath = getVirtMachinPath();

   // Abre xml
   ifstream xmlFile;
   xmlFile.open(vBoxPath);

   // Verifica abertura do xml
   if (xmlFile.fail()){
       cerr << "Erro ao abrir o ficheiro. \n\n";
       system("pause");
       //exit(1);
   }
   else{
       cerr << "Erro ao abrir o ficheiro";
       system("pause");
   }
   cout << vBoxPath << "\n\n" << vMachinesPath << "\n\n";
   system("pause");
}

No entanto encontro-me com outro problema...

Ao colocar o caminho para abertura do ficheiro xml ele da erro ao abrir.

O caminho está numa variavel e sempre que tento abrir por la dá erro.

Se colocar o destinho manualmente tudo funciona bem.

Sei qual será provavelmente o erro no entanto não o sei resolver.

Ao colocar o destino manualmento tenho de colocar duas barras...

Ex: c:\\users\\etc

Na variavel só tem uma barra. existe alguma forma de contornar a situação sem ter de colocar duas barras na string?

Edited by revised
Link to comment
Share on other sites

Por que não tentas algo deste género?

std::replace(path.begin(), path.end(), '\', '\\');

fontes:

http://www.cplusplus.com/reference/algorithm/replace/

http://stackoverflow.com/questions/4444486/replace-backward-slashes-with-forwards-slashes-or-double-backward-slashes-in-c

Edited by Colector Boy
Link to comment
Share on other sites

if (xmlFile.fail()){
       cerr << "Erro ao abrir o ficheiro. \n\n";
       system("pause");
       //exit(1);
   }
   else{
       cerr << "Erro ao abrir o ficheiro";
       system("pause");
   }

Tens a dar mensagem de erro nas duas condições? É este erro que tás a falar?

Precisas de pôr duas "barras" porque é um caractér reconhecido pela linguagem, logo duas dentro duma string significa que queres inserir \ .

Vê mais aqui http://stackoverflow.com/a/10220539/2990244

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.