C++/File/ifstream

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

Checking if open function is NULL and ifstream object"s fail

<source lang="cpp">

  1. include <fstream>
  2. include <iostream>

using namespace std; int main () {

  ifstream infile;
  infile.open("students.dat");
  cout << "(infile) = " << infile << endl;
  cout << "(infile.fail()) = " << infile.fail() << endl;
  return 0;

}


 </source>


Check status: EOF encountered, Non-Fatal I/O error, Fatal I/O error

<source lang="cpp">

  1. include <iostream>
  2. 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";

}



 </source>


Copy files with char buffer

<source lang="cpp">

  1. include <iostream>
  2. include <stdlib.h>
  3. include <fstream>

using namespace std; int main(int argc, char **argv)

{
  char buffer[1];
  
  ifstream input(argv[1], ios::in | ios::binary);
  if (input.fail())
   {
     cout << "Error opening the file " << argv[1];
     exit(1);
   }
  ofstream output(argv[2], ios::out | ios::binary);
  if (output.fail())
   {
     cout << "Error opening the file " << argv[2];
     exit(1);
   }


  do {
    input.read(buffer, sizeof(buffer));
    if (input.good())
      output.write(buffer, sizeof(buffer));
  } while (! input.eof());
  input.close();
  output.close(); 
}
 
   
 </source>


Display a file backwards on the screen: seekg(0, ios::end),tellg()

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; int main(int argc, char *argv[]) {

 if(argc!=2) {
   cout << "usage: REVERSE <filename>\n";
   return 1;
 }
 ifstream in(argv[1], ios::in | ios::binary);
 if(!in) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 char ch;
 long i;
 // go to end of file (less eof char)
 in.seekg(0, ios::end);
 i = (long) in.tellg(); // see how many bytes in file
 i -= 2;                // backup before eof
 for( ;i>=0; i--) {
   in.seekg(i, ios::beg);
   in.get(ch);
   cout << ch;
 }
 in.close();
 return 0;

}



 </source>


displays the contents of a file beginning with the location you specify on the command line.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>
  3. include <cstdlib>

using namespace std;

int main(int argc, char *argv[]) {

 char ch;
  
 if(argc!=3) {
   cout << "Usage: SHOW <filename> <starting location>\n";
   return 1;
 }
  
 ifstream in(argv[1], ios::in | ios::binary);
 if(!in) {
   cout << "Cannot open file.";
   return 1;
 }
  
 in.seekg(atoi(argv[2]), ios::beg);
  
 while(in.get(ch))
   cout << ch;
  
 return 0;

}


 </source>


file input with characters

<source lang="cpp">

  1. include <fstream>
  2. include <iostream>

using namespace std;

int main(){

  ifstream infile("Test.Txt");   
 
  cout << infile.rdbuf();        
  cout << endl;  
  return 0;  

}


 </source>


Get Third Type

<source lang="cpp">

  1. include <fstream>
  2. include <iostream>

using namespace std; int main() {

    const int max_count = 256;
    char line[ max_count ];
    ifstream in( "a.out" );
    while ( in.get( line, max_count ) ) {                      
       int count = in.gcount();                                
       cout << "num of bytes read:" << count << "\n";
       if ( count < max_count ) in.ignore();                 
    }
    return 0;

}


 </source>


ifstream.ignore: Ignore up to 10 characters or until first space is found.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; int main() {

 ifstream in("test");
 if(!in) {
   cout << "Cannot open file.\n";
   return 1;
 }
 /* Ignore up to 10 characters or until first space is found. */
 in.ignore(10, " ");
 char c;
 while(in) {
   in.get(c);
   if(in) 
      cout << c;
 }
 in.close();
 return 0;

}


 </source>


ignore( ) reads and discards characters from the input stream.

<source lang="cpp"> // ignore has this prototype: istream &ignore(streamsize num=1, int_type delim=EOF);

  1. include <iostream>
  2. include <fstream>

using namespace std;

int main() {

 ifstream in("test");
  
 if(!in) {
   cout << "Cannot open file.\n";
   return 1;
 }
  
 /* Ignore up to 10 characters or until first
    space is found. */
 in.ignore(10, " ");
 char c;
 while(in) {
   in.get(c);
   if(in) cout << c;
 }
  
 in.close();
 return 0;

}


 </source>


The ignore( ) Function

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; int main() {

 ifstream in("test");
 if(!in) {
   cout << "Cannot open file.\n";
   return 1;
 }
 in.ignore(10, " ");    // Ignore up to 10 characters or until first space is found.
 char c;
 while(in) {
   in.get(c);
   if(in) cout << c;
 }
 in.close();
 return 0;

}


 </source>


the use of both checking if the ifstream object used to call the open function is NULL and whether the ifstream object"s fail member function returns true

<source lang="cpp">

  1. include <fstream>
  2. include <iostream>

using namespace std; int main () {

  ifstream infile;
  infile.open("students.dat");
  cout << "(infile) = " << infile << endl;
  cout << "(infile.fail()) = " << infile.fail() << endl;
  return 0;

}


 </source>


Use while loop to read file content

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; main(int argc, char *argv[]) {

 char ch;   
    
 if(argc!=2) {   
   cout << "Usage: PR <filename>\n";   
   return 1;   
 }   
    
 ifstream in(argv[1]);   
 if(!in) {   
   cout << "Cannot open file";   
   return 1;   
 }   
    
 while(in) { // in will be 0 when eof is reached   
   in.get(ch);   
   cout << ch;   
 }   
    
 return 0;   

}


 </source>