C ++ determines that the file is empty.

Source: Internet
Author: User

Recently, I used C ++ file operations, in which I used to determine whether a file is empty. If it is empty, I will not read the file. After searching for the file from the Internet for a long time, I did not find it suitable. Finally, I chose the following method to judge whether the file is empty:

Code:
  1. Bool cmyfile: Empty ()
  2. {
  3. STD: String STR;
  4. M_stream> STR;
  5. If (Str. Empty ())
  6. {
  7. Return true;
  8. }
  9. M_stream.seekg (0, STD: IOS: Beg );
  10. Return false;
  11. }

Start to use m_stream.eof () for judgment, but this does not work, and the file cannot be determined to be empty. Because EOF () returns true only when the file Terminator 0xff is read, and the file Terminator is the next character of the last character of the file. Therefore, we need to read the file one more time to determine that the file is empty, which often cannot meet our needs. So I used the method above to replace it. It should be okay if there is no test yet. Of course there are other methods.

After testing the above method, there is a problem. After each empty determination, the file header is returned. If it is not empty, we will continue to read it, in this case, an infinite loop is generated. I made a slight modification and found a bug:

Code:
  1. Bool cmyfile: Empty ()
  2. {
  3. STD: String STR;
  4. M_stream> STR;
  5. If (Str. Empty ())
  6. {
  7. Return true;
  8. }
  9. // If it is not null, return to the beginning
  10. M_stream.seekg (-Str. Size (), STD: IOS: cur );
  11. Return false;
  12. }

In addition, I did a test on the methods of my classmates. It's no problem. It's much more concise than mine. Thank you, and you.

Code:
  1. Bool cmyfile: Empty ()
  2. {
  3. If (m_stream.peek () = EOF)
  4. {
  5. Return true;
  6. }
  7. Return false;
  8. }

Now, we know that the file is empty. Now I want to write the file. The problem is that I cannot write it. What's going on? After debugging, it was found that the file could not be written due to the reading of the file. Oh, I understand. When we finish reading the file, it will cause the file stream to become invalid. That's easy. Just clear the file stream sign.

Code:
  1. M_stream.clear ();

Now everything is normal. Haha.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.