cpp:stream_states
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cpp:stream_states [2019/05/21 07:01] – gthanos | cpp:stream_states [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| | **badbit** | | **badbit** | ||
| | **eofbit** | | **eofbit** | ||
| - | | **failbit** | + | | **failbit** |
| Οι ακόλουθες μέθοδοι ενημερώνουν για την εσωτερική κατάσταση του //stream// επιστρέφοντας μία //boolean// τιμή ως εξής: | Οι ακόλουθες μέθοδοι ενημερώνουν για την εσωτερική κατάσταση του //stream// επιστρέφοντας μία //boolean// τιμή ως εξής: | ||
| Line 18: | Line 18: | ||
| <code cpp check_file_open.cpp> | <code cpp check_file_open.cpp> | ||
| - | // getting the state of stream objects | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | |||
| + | using namespace std; | ||
| int main () { | int main () { | ||
| - | std::fstream | + | std::ifstream |
| - | fs.open ("hello.txt" | + | fs.open(" |
| if ( (fs.rdstate() & std:: | if ( (fs.rdstate() & std:: | ||
| - | std::cerr << "Error opening file 'hello.txt' | + | std::cerr << "Error opening file 'non-exist.txt' |
| - | return; | + | |
| } | } | ||
| </ | </ | ||
| + | |||
| + | ===== Έλεγχος των τιμών εισόδου (input validation) ===== | ||
| + | |||
| + | Με έλεγχο των παραπάνω //flags// μπορούμε να κάνουμε έλεγχο της εισόδου που εισάγει ο χρήστης σε ένα πρόγραμμα, | ||
| + | |||
| + | <code cpp input_validation.cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | using namespace std; | ||
| + | |||
| + | int main() { | ||
| + | double weight; | ||
| + | |||
| + | while (true) { | ||
| + | cout << "Enter your weight: "; | ||
| + | cin >> weight; | ||
| + | |||
| + | if (cin.fail()) { | ||
| + | // We failed to extract a double | ||
| + | cin.clear(); | ||
| + | cin.ignore(256, | ||
| + | continue; | ||
| + | } | ||
| + | |||
| + | if (weight <= 0) | ||
| + | continue; | ||
| + | |||
| + | break; | ||
| + | } | ||
| + | |||
| + | cout << "You weight is: " << weight << endl; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
cpp/stream_states.1558422116.txt.gz · Last modified: 2019/05/21 06:01 (external edit)
