revised Posted February 3, 2016 at 02:33 PM Report Share #593048 Posted February 3, 2016 at 02:33 PM (edited) 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 February 4, 2016 at 04:30 PM by pwseo syntax highlight Link to comment Share on other sites More sharing options...
revised Posted February 3, 2016 at 09:03 PM Author Report Share #593078 Posted February 3, 2016 at 09:03 PM Pessoal não precisa de ter o system("set userprofile") Se souberem outro método alternativo também pode ser... Link to comment Share on other sites More sharing options...
revised Posted February 4, 2016 at 06:16 PM Author Report Share #593182 Posted February 4, 2016 at 06:16 PM (edited) 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 February 4, 2016 at 06:16 PM by revised Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted February 6, 2016 at 10:48 AM Report Share #593219 Posted February 6, 2016 at 10:48 AM Bem apesar da ajuda aqui prestada consegui resolver o problema. fiquei super animado para te poder ajudar na tua dúvida, deixa só eu ir fazer uma sandwich e já venho ... IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Rui Carlos Posted February 6, 2016 at 04:43 PM Report Share #593230 Posted February 6, 2016 at 04:43 PM Respondendo à questão inicial, penso que a função popen seria a mais apropriada para o que pretendias. Rui Carlos Gonçalves Link to comment Share on other sites More sharing options...
Colector Boy Posted February 6, 2016 at 05:32 PM Report Share #593233 Posted February 6, 2016 at 05:32 PM (edited) 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 February 6, 2016 at 05:40 PM by Colector Boy Link to comment Share on other sites More sharing options...
He B TeMy Posted February 13, 2016 at 12:58 AM Report Share #593434 Posted February 13, 2016 at 12:58 AM 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now