I want to check if one file exists in two different paths. If the first one doesn't, check the second.
filepath1 = "/path1/file.txt"
filepath2 = "/path2/file.txt"
file_descriptor = open(filepath1)
if ( !file_descriptor.open() )
{
print("file 1 did not work");
//try file 2
file_descriptor = open(filepath2);
if( !file_descriptor.open() )
{
print("could not open file 2. exiting.");
return false;
}
}
//we get here and file_descriptor should point to a valid file
read_file(file_descriptor);
return true;
How can I avoid the nested if statement? Preferably, I'd like to not nest the if statements for readability.
The problem here is that if the first one does work, I don't want it to check the second if statement.
I thought about using:
goto(I want to avoid this)- boolean to check if one works (but now I'm introducing extra variables)