.


:




:

































 

 

 

 





 

 

. , . , , :

ifstream() ofstream() fstream()

{ { {

} } }

, . , ifstream ifs; ofstream ofs; fstream f1,f2;

, . :

ifstream(const char*name,int omode=ios::in,int prot=filebuf::openprot);

ofstream(const char*name,int omode=ios::out,int prot=filebuf::openprot);

fstream (const char*name,int omode, int prot=filebuf::openprot);

 

24. 3

 

, ifstream, ofstream fstream. . ifstream ofstream . . fstream : .

.

ifstream ifs1(ish.dan);

ofstream of1(rezult.dan);

ofstream of2(rezult2.txt,ios::app);

 

rezult2.txt (append).

 

fstream fil1(file1.txt,ios::in);

file1.txt .

fstream fil2(file2.dan,ios::out);

fstream fil3(file3.dan,ios::app);

 

file2.dan file3.dan .

open() ios. :

filebuf*open(const char*name,int mode, int prot=filebuf::openprot);

: (). ios.

num open_mode

{

in=0x01://open for reading

out=0x02://open for writing

ate=0x04://seek to end of file upon original//open

app=0x08://append mode

trunc=0x10://trancate file if already exist

nocreate=0x20:// open fails if file doesnt exist

noreplace=0x40:// open fails if file already exists

binary=0x80:// binary file

};

.

, :

fstream fin, fout;

fin.open(ish.dan, ios::in);

fout.open(rez.dan, ios::out);

 

. . :

fout.open(rez.dan, ios::out|ios::ate);

 

 

close, . :

fin.close ();

fout.close ();

, ( ) .

.

1 . ish.dan, rez.dan.

# include < iostream. h >

# include < fstream. h >

# include < math. h >

# include < stdlib. h >

void main ()

{

ifstream fin (ist. dan);

if (fin)

{

cout << ish. dan <<\ n ;

exit (1);

}

ofstream fout (rez. dan);

double x, y, xn, dx, xk;

fin >>xn>>dx>>xk;

fout << xn= <<xn<< dx= <<dx<< xk = <<xk<< \ n ;

for (x = xn; x <= xk; x += dx)

{

y = exp (-x);

fout << x = <<x<< y = << y << \ n;

}

fin. close ();

fout. close ();

}

2 .

. - , - . .

# include < iostream. h >

# include < fstream. h >

# include < stdlib. h >

# define N 2

# define M 3

class matr

{

float x [ N ] [ M ];

public:

void vvod (char * S);

void vivod (char * S);

friend matr operator + (matr & a, matr & b);

};

void matr:: vvod (char * S)

{

ifstream ifs (S);

if (ifs)

{

cout << << S << \ n ;

exit (1);

}

for (int i = Ø; i < N; i + +)

for (int j = Ø; j < M; j + +)

ifs >> x [ i ][ j ];

}

void matr:: vivod (char * S)

{

ofstream ofs (S);

for (int i = Ø; i < N; i ++)

for (int j = Ø; j < M; j ++)

ofs <<x [ i ][ j ];

}

matr operator + (matr & a, matr & b)

{

matr z;

for (int i = Ø; i < N; i ++)

for (int j = Ø; j < M; j ++)

z. x [ i ] [ j ] = a. x [ i ] [ j ] = b. x [ i ] [ j ];

return z;

}

void main ()

{

matr v, w, q

v. vvod (v. dan);

w. vvod (w. dan);

q = v + w;

q. vivod (q. dan);

}

v w v.dan w.dan. q q.dan.

 

24.5 /

, . :

friend ifstream & operator >> (ifstream & ifs, & );

friend ofstream & operator << (ofstream & ofs, & );

 

. , , v hod. dan.

v [ 2 ] [ 3 ] w [ 2 ] [ 3 ]. v hod. dan. , v hod. dan :

1 2 3

4 5 6

-1 -2 -3

-4 -5 -6

v, w.

# include < iostream. h >

# include < fstream. h >

# include < stdlib.h >

# define N 2

# define M 3

class matr

{

float x [ N ] [ M ];

friend ifstream & operator >> (ifstream & ifs, matr & x);

friend ofstream & operator << (ofstream & ofs, matr & a);

friend matr operator + (matr & a, matr & b);

};

ifstream & operator >> (instream & ifs, matr & x)

{

for (int I = Ø; I < N; I ++)

for (int j = Ø; j < M; j ++)

ifs >>x. x [ I ] [ j ]; / * * /.

return ifs; // !

}

ofstream & operator << (ofstream & ofs, matr & a)

{

for (int I = Ø; I < N; I ++)

for (int j = Ø; j < M; j ++)

ofs << a. x [ I ] [ j ];

return ofs;

}

matr operator + (matr & a, matr & b)

{

matr v;

for (int I = Ø; I < N; I ++)

for (int j = Ø; j < M; j ++)

v. x [ I ] [ j ] = a. x [ I ] [ j ] + b. x [ I ] [ j ];

return v;

}

void main ()

{

matr w, v;

ifstream fvh (v hod. dan);

fvh >> v >> w; // * >>. v, w */

matr q;

q = v + w; // + ().

ofstream viv (q. dan);

viv << q; // q q.dan.

// << ().

}

25

 

+ + : , .

 





:


: 2015-11-05; !; : 660 |


:

:

, , 1:10
==> ...

1500 - | 1445 -


© 2015-2024 lektsii.org - -

: 0.025 .