You are on page 1of 27

&

&

(201314)
gepap@aueb.gr

&

:



/

:2013
2

&

2:

(binary)

,Java,
(read),(write/append),

CC++
,,
Java

&

,
(
Java)
ios_base
:
ios
istream

istringstream

iostream

fstream

ifstream

ostream

ofstream

ostringstream

stringstream

&

[i/o][f/string]stream
i/o:input/output.io :

f/string:stream:file/string.

string
buffer

&


,,
:


01011001

00011000

00001111

00000100

00000001

eof
endoffile

&

,
,
:
11001000

200

502480480
00110010

00110000

00110000

&

fstream/ifstream/ofstream:
#include <fstream>
#using namespace std;

string filename = log.data;


ofstream file(filename, ofstream::out | ofstream::binary);
Mode:
ifstream file2(input_report.txt );
Mode:
8

&

ifstream ( const char* filename, ios_base::openmode mode );

ifstream ();
open( const char* filename, ios_base::openmode mode );

mode,:
ios_base::binary:

ios_base::app:(appends)

ios_base::in/out :
().ifstream
ios_base::inios_base::out

9

&

fstream ( const char* filename, ios_base::openmode mode );

fstream ();
open( const char* filename, ios_base::openmode mode );

mode,:
ios_base::binary:

ios_base::app:(appends)

ios_base::in/out :
().fstream
ios_base::outios_base::in

10

&

(ifstream,ofstream,
fstream),close()

11

&

bool fail():true
(..,
)
bool eof() :true

bool good() :true


12

&


bytes,char (1byte)
:
ostream& put(charc):byte
ostream& write(const char*s,streamsize n):
blockbytes()

13

&

bytes
,char (1byte)
() :
int get():byte
istream&get(char&c)
istream& read(char*s,streamsize n):nbytes
()
int peek():byte
(inplace)!
get

14

&

.
n
endofline
(\n)
(.delim):
istream&getline (char*s,streamsize n);
istream&getline (char*s,streamsize n,chardelim );

15

&

std::streampos tellg()

istream&seekg (std::streampos pos)


/
pos

16

&

#include <fstream>
//
#include <iostream> //
using namespace std;
void writeShortValue(string filename, unsigned short & value){
ofstream file(filename, ios::binary);
if (!file) {
cerr << "Cannot open file" << endl;
return;
}
file.write((char*)&value, 2); // value
// 2 bytes.
// 2 bytes short
if (file.fail()){
cerr << "Could not write data" << endl;
}
file.close();
return;
}

17

&

#include <fstream>
#include <iostream>
using namespace std;
unsigned short readShortValue(string filename){
unsigned short value; // short: 2 bytes
ifstream file(filename, ios::binary);
if (!file) {
cerr << "Cannot open file" << endl;
return 0;
}
file.read((char*)&value, 2); // value
// 2 bytes.
// 2 bytes
if (file.fail()){
cerr << "Could not read data" << endl;
return 0;
}
file.close(); return value;
}
18

&

19

&

,(i/o)2
:
>>():

,
(..
1234.05float1234.05)
<<():

20

&

iostream
Tocout
(..)
cin

cerr

21

&

istream&operator>>(bool&val);

ostream&operator<<(bool val);

istream&operator>>(short&val);

ostream&operator<<(shortval);

istream&operator>>(unsignedshort&val);

ostream&operator<<(unsignedshortval);

istream&operator>>(int&val);

ostream&operator<<(int val);

istream&operator>>(unsignedint&val);

ostream&operator<<(unsignedint val);

istream&operator>>(long&val);

ostream&operator<<(longval);

istream&operator>>(unsignedlong&val);

ostream&operator<<(unsignedlongval);

istream&operator>>(float&val);

ostream&operator<<(longlong val);

istream&operator>>(double&val);

ostream&operator<<(unsignedlonglong val);

istream&operator>>(longdouble&val);

ostream&operator<<(floatval);

istream&operator>>(void*&val);

ostream&operator<<(doubleval);

istream&operator>>(streambuf*sb );

ostream&operator<<(longdoubleval);

istream&operator>>(istream&(*pf)(istream&));

ostream&operator<<(void*val);

ostream&operator<<(streambuf*sb );

22

&

void writeFloat(string filename, float & value)


{
ofstream file(filename);
if (!file) {
cerr << "Cannot open file" << endl;
return;
}
setlocale(LC_ALL,"Greek");

(
)

file << " " << value << endl;


if (file.fail()){
cerr << "Could not write data" << endl;
}

locale

file.close();
return;
}

23

&


:
2048.1

,
!

24

&

float readFloat(string filename) {


float value;
ifstream file(filename);

if (!file) {
return 0.0f;
}
setlocale(LC_ALL,"Greek");
stringbuf buffer;
file.get(buffer, '');
file.get(buffer, ' ');
file >> value;

.Ostringbuf (stringbuffer)

buffer

cout << buffer.str() << " " << value;


if (file.fail()){
file.close(); return 0.0f;
}
file.close();
return value;
}

(1000.1
1000.1)
25

&

streamsize precision(streamsize prec);

streamsize width(streamsize wide);

charfill(charfillch);

(width)
26

&


width:
int n = -77;
std::cout.width(6); std::cout << std::internal << n << '\n'; 77
std::cout.width(6); std::cout << std::left << n << '\n';
-77
-77
std::cout.width(6); std::cout << std::right << n << '\n';


:
std::fixed
std::scientific

:
std::dec
std::hex
std::oct
27