Reputation: 1
I'm trying to open a file in my program to import some information from it. The relevant part of the code is this one:
Airport::Airport(string& apt) {
ifstream datasid;
ifstream datastar;
ICAO = apt;
if (ICAO == "LEPA"){ //fill map with points and their departure
datasid.open("LEPASID.txt");
datastar.open("LEPASTAR.txt");
}
else if (ICAO == "LEAL"){ //fill map with points and their departure
datasid.open("LEALSID.txt");
datastar.open("LEALSTAR.txt");
}
else {
cout << "El aeropuerto no se encuentra en la base de datos." << endl;
correct = false;
}
if (datasid.fail() or datastar.fail()) cout << "Se ha producido un error al leer los datos del aeropuerto" << endl;
When I run the program, I get an error:
Se ha producido un error al leer los datos del aeropuerto
Meaning that datasid
or datastar
failed.
The files are in the same directory as the source files, and I checked that the names are correct.
Upvotes: 0
Views: 380
Reputation: 1
I moved all the files, including the sources, executables etc. to a new directory. It's working now. I assume there was some kind of problem with the old directory.
Upvotes: 0
Reputation: 451
I have run that program in Visual Studio using main()
and it works fine for me after fixing a few problems:
Here is my working code. This works for me. Check the differences with yours (I added the std::couts
to check which if
is activated). If it still doesn't works, probably the issue is with your variable ICAO
(it is not LEAL or LEPA), with your Airport
class, or maybe you don't have the .txt files in the correct directories.
#include <string>
#include <fstream>
#include <iostream>
int main() {
std::string apt = "LEPA";
std::ifstream datasid;
std::ifstream datastar;
std::string ICAO = apt;
if (ICAO == "LEPA") {
datasid.open("LEPASID.txt");
datastar.open("LEPASTAR.txt");
std::cout << "LEPA OK";
}
else if (ICAO == "LEAL") {
datasid.open("LEALSID.txt");
datastar.open("LEALSTAR.txt");
std::cout << "LEAL OK";
}
else {
std::cout << "El aeropuerto no se encuentra en la base de datos." << std::endl;
bool correct = false;
}
if (datasid.fail() || datastar.fail())
std::cout << "Se ha producido un error al leer los datos del aeropuerto"
<< std::endl;
Upvotes: 2