A friend asks me for Program , Related Code As follows: String S; Vector <string>; Ifstream infile ("F: // myreceivefiles // fillcompo_kp.mod", IOS: In ); While (Getline (infile, S )) Vs. push_back (s ); Cout <vs. Size () <Endl; Infile. Close (); Infile. Open ("F: // myreceivefiles // fillcompo_line.mod", IOS: In ); While (Getline (infile, S )) Vs. push_back (s ); Infile. Close (); Cout <vs. Size () <Endl; Return 0; She said that only the first file (fillcompo_kp.mod) can be opened, and the second file (fillcompo_line.mod) cannot be opened ). I tested this
This problem exists, but this code does not look wrong. My preliminary judgment is what operations may be required after the file is closed. So I searched the internet for information, searched for a while, and finally
Find one: closing the Stream does not change the internal status of the flow object. If the last read/write operation fails, the object State remains in error mode until clear is executed.
Operation until the stream is restored. After clear is called, it is like re-creating this object. If the programmer needs to reuse the file stream to read and write multiple files, it must be called before reading another file
Clear clears the status of the stream. Therefore, you must add the following code after calling the close function of the file stream object: infile. Clear (IOs: goodbit);. The Code is as follows: String S; Vector <string>; Ifstream infile ("F: // myreceivefiles // fillcompo_kp.mod", IOS: In ); While (Getline (infile, S )) Vs. push_back (s ); Cout <vs. Size () <Endl; Infile. Close (); Infile. Clear (IOs: goodbit); // IOs: goodbit It indicates no error. You can continue the input or operation. Infile. Open ("F: // myreceivefiles // fillcompo_line.mod", IOS: In ); While (Getline (infile, S )) Vs. push_back (s ); Infile. Close (); Cout <vs. Size () <Endl; Return 0;
Attached http://hi.baidu.com/ctralt/blog/item/cde79fec87f841302697911c.html
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.