0

I have the code below. If I enter the file's name immediately, it finds the file without a problem, and I do not have do to anything. If I, however, types in a file it does not locate first, it will not find the file in the coming sequences, even if it exists. Does anybody understand what is wrong? The file is in the same folder.

string fileName;
cout << "Ingrese nombre archivo de carga: " << endl;
cin >> fileName;

if (fileName == "0")
    return false;


    ifstream infile(fileName); //here it should be filename
    while (true) {
        if (infile) {
            cout << "Cargando directorio..." << endl;
            break;
        } else {
            cout << "ERROR: No se pudo abrir el archivo." << endl;
        }
        cout << "Ingrese nombre archivo de carga: " << endl;
        cin >> fileName;
        if (fileName == "0")
            return false;


        ifstream infile(fileName); 
    }
Filip
  • 71
  • 1
  • 7

1 Answers1

1
ifstream infile(fileName); //here it should be filename

This declares a variable named infile.

    ifstream infile(fileName); 
}

This declares a completely new variable, also named infile. Its zone of visibility lasts from the point of declaration to the closing brace of the enclosing block (that is, not for very long).

You want to use the old variable instead. Try calling its open method.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Oh dear, I can't believe it was such a small error. Thank you very much, worked like a charm. – Filip May 26 '17 at 00:05