C++/File/File Status

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Check file status

 
#include <iostream>
#include <fstream>
using namespace std;
void checkstatus(ifstream &in);
int main(int argc, char *argv[])
{
  if(argc!=2) {
    cout << "Usage: DISPLAY <filename>\n";
    return 1;
  }
  ifstream in(argv[1]);
  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }
  char c;
  while(in.get(c)) {
    cout << c;
    checkstatus(in);
  }
  checkstatus(in);  // check final status
  in.close();
  return 0;
}
void checkstatus(ifstream &in)
{
  ios::iostate i;
  i = in.rdstate();
  
  if(i & ios::eofbit) 
    cout << "EOF encountered\n";
  else if(i & ios::failbit)
    cout << "Non-Fatal I/O error\n";
  else if(i & ios::badbit)
    cout << "Fatal I/O error\n";
}


End of file sign

 
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
  char ch;
  if(argc!=2) {
    cout << "PR: <filename>\n";
    return 1;
  }
  ifstream in(argv[1]);
  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }
  while(!in.eof()) { 
    in.get(ch);
    // check for error
    if(!in.good() && !in.eof()) {
      cout << "I/O Error...terminating\n";
      return 1;
    }
    cout << ch;
  }
  in.close();
  return 0;
}


I/O Status

  
/*
Name                 Meaning
ios::goodbit         No error bits set
ios::eofbit          1 when end-of-file is encountered; 0 otherwise
ios::failbit         1 when a (possibly) nonfatal I/O error has occurred; 0 otherwise
ios::badbit          1 when a fatal I/O error has occurred; 0 otherwise
*/
#include <iostream>
#include <fstream>
using namespace std;
   
void checkstatus(ifstream &in);
   
int main(int argc, char *argv[])
{
  if(argc!=2) {
    cout << "Usage: Display <filename>\n";
    return 1;
  }
   
  ifstream in(argv[1]);
   
  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }
   
  char c;
  while(in.get(c)) {
    if(in) cout << c;
    checkstatus(in);
  }
   
  checkstatus(in);  // check final status
  in.close();
  return 0;
}
   
void checkstatus(ifstream &in)
{
  ios::iostate i;
   
  i = in.rdstate();
   
  if(i & ios::eofbit)
    cout << "EOF encountered\n";
  else if(i & ios::failbit)
    cout << "Non-Fatal I/O error\n";
  else if(i & ios::badbit)
    cout << "Fatal I/O error\n";
}


The string pointed to by mode determines how the file will be opened. The following table shows the legal values for mode. (Strings like ?+b?may also be represented as ?b+.?

  
Mode        Meaning
r           Open a text file for reading.
w           Create a text file for writing.
a           Append to a text file.
rb          Open a binary file for reading.
wb          Create a binary file for writing.
ab          Append to a binary file.
r+          Open a text file for read/write.
w+          Create a text file for read/write.
a+          Append or create a text file for read/write.
r+b         Open a binary file for read/write.
w+b         Create a binary file for read/write.
a+b         Append or create a binary file for read/write.