C++/File/ifstream
Содержание
- 1 Checking if open function is NULL and ifstream object"s fail
- 2 Check status: EOF encountered, Non-Fatal I/O error, Fatal I/O error
- 3 Copy files with char buffer
- 4 Display a file backwards on the screen: seekg(0, ios::end),tellg()
- 5 displays the contents of a file beginning with the location you specify on the command line.
- 6 file input with characters
- 7 Get Third Type
- 8 ifstream.ignore: Ignore up to 10 characters or until first space is found.
- 9 ignore( ) reads and discards characters from the input stream.
- 10 The ignore( ) Function
- 11 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
- 12 Use while loop to read file content
Checking if open function is NULL and ifstream object"s fail
#include <fstream>
#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;
}
Check status: EOF encountered, Non-Fatal I/O error, Fatal I/O error
#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";
}
Copy files with char buffer
#include <iostream>
#include <stdlib.h>
#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();
}
Display a file backwards on the screen: seekg(0, ios::end),tellg()
#include <iostream>
#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;
}
displays the contents of a file beginning with the location you specify on the command line.
#include <iostream>
#include <fstream>
#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;
}
file input with characters
#include <fstream>
#include <iostream>
using namespace std;
int main(){
ifstream infile("Test.Txt");
cout << infile.rdbuf();
cout << endl;
return 0;
}
Get Third Type
#include <fstream>
#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;
}
ifstream.ignore: Ignore up to 10 characters or until first space is found.
#include <iostream>
#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;
}
ignore( ) reads and discards characters from the input stream.
// ignore has this prototype: istream &ignore(streamsize num=1, int_type delim=EOF);
#include <iostream>
#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;
}
The ignore( ) Function
#include <iostream>
#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;
}
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
#include <fstream>
#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;
}
Use while loop to read file content
#include <iostream>
#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;
}