슬라이드 1

Download Report

Transcript 슬라이드 1

C++ Programming:
chapter 6 – iostream
2014, Spring
Pusan National University
Ki-Joune Li
http://isel.cs.pnu.edu/~lik
1
PNU

fstream, ofstream, ifstream classes
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
ifstream myfile ("example.txt”, );
if (myfile.is_open()) {
while ( myfile.good() ) {
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file”;
return 0;
}
PNU
Flag
Explanations
ios::in
Open for input operations.
ios::out
Open for output operations.
ios::binary
Open in binary mode.
ios::ate
Set the initial position at the end of the file. If this flag is not set
to any value, the initial position is the beginning of the file.
ios::app
All output operations are performed at the end of the file,
appending the content to the current content of the file.
This flag can only be used in streams open for output-only operations.
ios::trunc
If the file opened for output operations already existed before, its
previous content is deleted and replaced by the new one.
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);
3

PNU
Set Position: tell and seek in C-language
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
long begin,end;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << "
bytes.\n";
return 0;
}
ios::beg offset from the beginning
ios::cur offset from the current
ios::end offset from the end
functions
Explanations
pos_type tellg(),
pos_type tellp()
Offset (g: for input file, p: for output file stream)
seekg(pos_typ),
seekp(pos_typ)
Set position from the beginning
seekg(pos_typ, direction)
seekp(pos_typ, direction)
Set position with direction
4
PNU

Binary file i/o stream: read and write in C-Language
// reading a complete binary file
#include <iostream>
#include <fstream>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main ()
{
ifstream file ("example.bin", ios::in | ios::binary | ios::ate);
if (file.is_open()) {
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read(memblock, size);
file.close();
cout << "the complete file content is in memory";
delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}
5
PNU

Set flag: set flags for output stream
#include <iostream>
using namespace std;
int main () {
cout.setf(ios::hex, ios::basefield );
// set hex as the basefield
cout << 100 << endl;
cout.setf(ios::dec, ios::basefield );
// set decimal as the basefield
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<3.141592;
format flag value
mask field bitmask
cout.set
left, right or internal
adjustfield
return 0;
}
dec, oct or hex
basefield
scientific or fixed
floatfield
cout.width(5);
cout << 12.3456 << setw(4) << 12.3456;
cout << 12.3456 << setprecision(2) << 12.3456;
6
PNU

IOS hierarchy
7
PNU

Checking File Status
functions
Explanations
bad()
returns true if a reading or writing operation fails
fail()
returns true in the same cases as bad(), but also in the case that a
format error happens, like when an alphabetical character is extracted
when we are trying to read an integer number.
eof()
returns true if a file open for reading has reached the end.
good()
returns false in the same cases in which calling any of the previous
functions would return true.
8