Open a file from another directory with C ++ in GNU / Linux

0

My intention is to select a text file from the text / directory, but when I select it, I get the error.

ifstream Archivo;
system("clear");
cout << "Elige el archivo que quieres leer" << endl;
system("cd texto/ && ls --color");
cout << "Abrir: ";
cin >> archivo_elegir;          
Archivo.open(archivo_elegir);
if(Archivo.fail()){
    cout << "Error al abrir el archivo." << endl;
    exit(1);
}
else{
    cout << "OK";
}                       
Archivo.close();

If I had the txt in the same executable directory and changed the line

system("cd text/ && ls --color");

for

system("ls --color");

the program works.

    
asked by akko 13.12.2016 в 05:01
source

1 answer

1

You are changing directories with the system command, at OS level but in your program, when opening the file you are opening it without giving it the path "text /". Try: "File.open (" text / "+ choose_file".

    
answered by 14.12.2016 / 11:40
source