39 lines
568 B
C++
39 lines
568 B
C++
// https://en.cppreference.com/w/cpp/utility/optional
|
|
|
|
#include<iostream>
|
|
#include<optional>
|
|
|
|
std::optional<FILE *> ffopen(char const* fname)
|
|
{
|
|
FILE *fin = fopen(fname, "r");
|
|
if(fin==NULL)
|
|
{
|
|
return {};
|
|
}
|
|
return fin;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
std::optional<FILE *> rv = ffopen("/home/famubu/hi.txt");
|
|
char str[30];
|
|
if (rv.has_value())
|
|
{
|
|
fgets(str, 29, rv.value());
|
|
puts(str);
|
|
printf("YES\n");
|
|
}
|
|
else
|
|
{
|
|
printf("NO\n");
|
|
}
|
|
}
|
|
|
|
/*
|
|
$ g++ optional.cpp --std=c++17 -o a.out
|
|
$ ./a.out
|
|
0
|
|
100
|
|
*/
|
|
|