File input / output in C (4)
Original: Ilia Yordanov, Loobian@cpp-home.com
Detect input / output status flag
Here I don't plan to explain the meaning of the word "flag", but if you really don't understand the concept about this, then you will read it later, you will get some understanding, I also believe You can also understand the theory of this part. Despite this, if you still don't understand the meaning of the flag in C , I recommend you to read some information about this topic.
Ok, let's get started.
The input / output system responsible in C includes record information about the results of each input / output operation. These current status information is included in the object of the IO_STATE type. IO_STATE is an enumeration type (like Open_Mode), the following is the value it contains (the translation: the first list as the enumeration value in the table, the second list is the corresponding meaning of this value):
godbit
No error
Eofbit
Document
Failbit
Non-fatal input / output error
Badbit
Input / output error
There are two ways to get the status information of the input / output. One way is to return the RDState () function, which will return the error tag of the current state (mentioned in the table below). For example, if there is no error, RDState () will return to Goodbit.
Another method is to use any of the following functions to detect the corresponding input / output status:
Bool bad ();
BOOL EOF (); / / Remember it? "Constantly read the file content until the end of the file!"
Bool fail (); //, this is also an old friend ... Testing if an open operation is successful
Bool good ();
If the Badbit flag is marked (the original text is "if The Badbit Flag IS Up", "IS UP" is translated into "marking", which means that the BADBIT corresponds to the error, the BADBIT status is set as the current error. State, the following, the BAD () function returns true; if the Failbit flag is marked, the fail () function returns true; if there is no error (the goodbit flag is marked), the good () function returns true; if The operation has arrived at the end of the file (EOFBIT is marked), then the EOF () function returns True.
If the error occurs, you have to clear these error status so that your program can continue to run correctly - if you are going on. To clear the wrong status, you need to use the CLEAR () function. This function takes a parameter, which is a logo value you will be set to the current state. If you want your program to "clear and cool", as long as IOS :: goodbit is used as the argument. You will see the sample code in the following.
I will show you sample code to consolidate the theoretical knowledge you have learned.
Example 1: Simple state detection
// In practical applications, filestream can be replaced with the file flow handle you used accordingly.
IF (filestream.rdstate () == ios :: EOFbit)
Cout << "End of file! / n";
IF (filestream.rdstate () == ios :: badbit)
COUT << "Fatal I / O Error! / N";
IF (filestream.rdstate () == iOS :: Failbit)
COUT << "Non-Fatal I / O Error! / N"; if (filestream.rdstate () == ios :: goodbit)
COUT << "no errors! / n";
Example 2: Clear () function
#include
void main ()
{
OFSTREAM FILE1 ("file2.txt"); // Establish File2.txt
File1.close ();
// The following detection code will return an error, because I use ios :: NorePlace Open mode
// It mode returns an error when trying to open an existing file
OFSTREAM TEST ("File2.txt", ios :: Noreplace;
// The last line will cause ios :: Failbit error, we will show it out
IF (Test.rDState () == iOS :: Failbit)
Cout << "error ...! / n";
Test.clear (iOS :: Goodbit); // Reset the current state to iOS :: Goodbit
IF (Test.rdState () == iOS :: Goodbit) // Whether the detector has been properly implemented
COUT << "Fine! / N";
Test.clear (ios :: eofbit); // Set the status sign to iOS :: EOFbit. No practical use.
IF (Test.rDState () == iOS :: EOFBIT) // Detects whether to set up COUT << "EOF! / N";
Test.close ();
}
In addition to using the tag value, you can also use a function (translation: refer to BAD (), EOF (), Fail (), and good () these functions, both actually the same - all detection Whether a tag is marked. I have already introduced these functions, I will no longer be repeated. If you are not very sure how to use them, then you will re-review the part of this tutorial that I have demonstrated how to detect a file to open the operation. There I used a fail () function there.