, . <iomanip>.
setbase(int n) | ( = 8, 16, 10 0). 0 (, , 8- 16- ); |
resetiosflags(long) | , ; |
setiosflags(long) | , 1; |
setfill (int) | - , ; |
setprecision(int) | ( fixed) ( scientific); |
setw(int) | . |
. , ,
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
const int n=2;
//
struct Tstudent
{
char FIO[20];
float bal;
}StudetnPotok[n]={"Petrov",3.5,"Sidorov",5.0/3};
int main (int argc, char * const argv[])
{
for(int i=0;i<n;i++)
{
cout<<resetiosflags(ios::right);// cout.unsetf(ios::right);
cout<<setiosflags(ios::left); // cout.setf(ios::left);
cout<<setfill('.'); // cout.fill('.');
cout<<setw(15); // cout.width(15);
cout<<setiosflags(ios::fixed); // cout.setf(ios::fixed);
cout<<setiosflags(ios::scientific);// cout.setf(ios::scientific);
cout<<resetiosflags(ios::fixed);// cout.unsetf(ios::scientific);
cout<<setprecision(3); // cout.setf(ios::scientific);
cout <<StudetnPotok[i].FIO;//
cout<<setw(10); // cout.width(10)
cout<<setiosflags(ios::right);// cout.setf(ios::right);
cout<<setfill('_'); //cout.fill('_')
cout<<StudetnPotok[i].bal<<'\n';
}
cin.get();
return 0;
}
:
Petrov........._3.500e+00
Sidorov........_1.667e+00
.
#include <iostream>
#include <iomanip>
using namespace std;
const int n=2;
struct Tstudent
{
char FIO[20];
float bal;
}StudetnPotok[n]={"Petrov",3.5,"Sidorov",5.0/3};
int main (int argc, char * const argv[])
|
|
{
for(int i=0;i<n;i++)
{
cout<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setfill('.')
<<setw(15)<<StudetnPotok[i].FIO;
cout<<setprecision(3)<<setw(10)<<setiosflags(ios::right|ios::fixed)
<<setfill('_')<<StudetnPotok[i].bal<<'\n';
}
cin.get();
return 0;
}
:
Petrov........._______3.500
Sidorov........______1.667
, ,
,
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int n=2;
//
struct Tstudent
{
char FIO[20];
float bal;
}Stud[n]={"Petrov",3.5,"Sidorov",5.0/3};
void myOut(ostream &out,Tstudent *StudetnPotok,int n)
{
for(int i=0;i<n;i++)
{
cout<<resetiosflags(ios::right)<<setiosflags(ios::left)
<<setfill('.')<<setw(15)<<StudetnPotok[i].FIO;
cout<<setw(10)<<setiosflags(ios::right|ios::fixed)
<<setfill('_')<<StudetnPotok[i].bal<<'\n';
}
}
int main (int argc, char * const argv[])
{
// :
myOut(cout,Stud,n);
ofstream fileout("o.txt");
myOut(fileout,Stud,n);
return 0;
}
>> << ( ). , istream.
gcount() | , ; |
get() | EOF; |
get () | , , ; |
get(buf,num,lim='\n') | num-1 ( lim) buf. lime ('\0'). lim . ; |
getline(buf, num, lim='\n') | get, buf lim; |
ignore(num = 1, lim = EOF) | , num , lim. ; |
peek() | EOF, ; |
putback(c) | , ; |
read(buf, num) | num ( , num) buf ; |
readsome(buf, num) | num ( , num) buf ; |
seekg(pos) | pos; |
seekg(offs, org) | offs , , org: ios::beg ( ), ios::cur ( ) ios::end ( ); |
tellg() | ; |
unget() | . |
ostream :
|
|
flush() | ; |
put() | ; |
seekg(pos) | pos; |
seekg (offs, org) | offs , , org: ios::beg ( ), ios::cur ( ) ios::end ( ); |
tellg() | ; |
write(buf, num) | num buf . |
ios state, :
enum io_state {
goodbit = 0x00, //
eofbit = 0x01, //
fail bit = 0x02, //
badbit = 0x04, // ,
//
hardfail = 0x08 //
}:
:
int rdstate() | ; |
int eof() | , eofbit; |
int fail() | , failbit, badbit hardfail; |
int bad() | , badbit hardfail; |
int good() | , ; |
void clear(int = 0) | , 0; |
operator void*() | , ; |
operator!() | , . |
.
// , flag;
if(stream_obj.rdstate() & ios::flag)
// flag:
stream_obj.clear(rdstate() & ~ios::flag)
// flag:
stream_obj.clear(rdstate() | ios::flag)
// .flag :
stream_obj.clear(ios::flag)
// :
stream_obj.clear()
vo1d*() , 0. :
while (stream_obj){
// , /
}
, , . , , , .
, , , .
: ifstream ; ofstream ; fstream .
i stream, ostream iostream , , , , , . .
|
|
:
- ;
- ;
- (/); - ;
- .
, .
- , :
ifstream(); ofstream(); fstream();
- , :
ifstream(const char *name. int mode = ios::in);
ofstream(const char *name, int mode - ios::out | ios::trunc);
fstream(const char *name. int mode - ios::in | ios::out);
. , , , ios:
enum open_mode
{
in = 0x01, //
out = 0x02, //
ate = 0x04, //
= 0x08, //
trunc = 0x10, // ,
nocreate = 0x20, // ,
noreplace = 0x40, // ,
binary = 0x80 //
};
, open, , , :
ifstream inpf("input.txt",, ios::in|ios::nocreate); //
if (!inpf)
{
cout << " "; return 1;
}
ofstream f;
f.open("output.txt"); // open
if (!f)
{
cout << " "; return 1;
}
, , .
( ):
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char text[81], buf[81];
cout << " :";
cin >> text;
ifstream f(text, ios::in);
if (!f)
{
cout << " "; return 1;
}
while (!f.eof())
{
f.getline(buf, 81);
cout << buf << endl;
}
cin.get();
cin.get();
return 0;
}
close(), , , .
}
close(), , , .
. (, ) .
:
in.write((char*)&p,sizeof(p)); ,
out.read((char*)&p,sizeof(p)); ,
n , out - ; ( ).
in.write((char*)p,sizeof(*p)*n); ,
|
|
out.read((char*)p,sizeof(*p)*n); ,
n .
, ,
#include <iostream>
#include <fstream>
using namespace std;// std
void createTestFile(const char* fileName)
{
ofstream out(fileName);
out<<"test file test file \ntest file ";
}
int main()
{
cout<<""<<endl;
createTestFile("testFile.txt");
ifstream innn("testFile.txt");//
if (!innn)
{
cerr<<" \n";
return 1;
}
int n=0;
while (!innn.eof())//
//
{
innn.get();// 1 .
//
n++;// 1
}
cout<<n<<endl;//
n=0;//
char str[300];//
//
innn.clear();//
//
//
//
//
innn.seekg(0);//
while (!innn.eof())//
//
//
{
innn>>str;//
// ,
//
n++; // 1
}
cout<<n<<endl;//
//
n=0;//
innn.clear();
innn.seekg(0);
while (!innn.eof())
//
{
innn.getline(str,300);// getline
//
// '\n'
// str
n++;// 1
}
cout<<n<<endl;//
return 0;
}
, , . :
istringstream ; ostringstream ; stringstream .
<sstream> i stream, ostream iostream .
. .
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
const int n=2;
struct Tstudent
{
char FIO[20];
float bal;
}Stud[n]={"Petrov",3.5,"Sidorov",5.0/3};
int main ()
{
ostringstream mystr;
for(int i=0;i<n;i++)
{
mystr<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setfill('.')
<<setw(15)<<Stud[i].FIO;
mystr<<setw(10)<<setiosflags(ios::right|ios::fixed)<<setfill('_')<<Stud[i].bal<<'\n';
}
cout<<mystr.str();// .str()-
// string
//
char *str=new char [mystr.str().size()+1];// size()
// string
strcpy(str,mystr.str().c_str()); // c_str() string,
// char*
cout<<str<<endl;
delete[]str;
cin.get();
return 0;
}
2.4
. : 15 , 8 . ., :
................................3
.............................5
. , , .
|
|
.
2.6
1
STUDENT, : - ;
- ;
- ( ). , :
- , STUDENT; ;
- , , 4.0;
- , .
2
STUDENT, ; - ;
- ;
- ( ). , :
- , STUDENT; ;
- , 4 5;
- , .
3
STUDENT, :
- ; - ;
- ( ). , :
- , STUDENT; ;
- , 2;
- , .
4
AEROFLOT, :
- ;
- ;
- .
, :
- , AEROFLOT; ;
- , , , ;
- ,
5
AEROFLOT, :
- ;- ;
- .
, :
- , AEROFLOT; ;
- , , ;
- , .
6
WORKER, :
- ;
- ;
- .
, :
- , WORKER; ;
- , , ;
- , .
7
TRAIN, :
- ;
- ;
- .
, :
- , TRAIN; ;
- , ;
- , .
8
TRAIN, :
- ;
- ;- .
, :
- , TRAIN; ;
- , , ;
- , .
9
TRAIN, :
- ;
- ;
- .
, :
- , TRAIN; ;
- , ;
- , .
10
MARSH, :
- ;
- ;
- .
, :
- , MARSH; ;
- , ;
- , .
11
MARSH, :
- ;
- ;
- .
, :
- , MARSH; ;- , , ;
- , .
12
NOTE, :
- , ;
- ;
- ( ).
, :
- , NOTE; ;
- , ;
- , .
13
NOTE, :
- , ;
- ;
- ( ).
, :
- , NOTE; ;
- , , ;
- , .
14
NOTE, :
- , ; - ;
- ( ).
, :
- , NOTE; ;
- , ;
- , .
15
ZNAK, :
- , ;
- ;
- ( ).
, :
- , ZNAK; ;
- , ;
- , .
16
ZNAK, :
- , ;
- ;
- ( ).
, :
- , ZNAK; ;
- , , !
- , .
17
ZNAK, : - , ;
- ;
- ( ).
, :
- , ZNAK; ;
- , , ;
- , .
18
PRICE, :
- ;- , ;
- .
, :
- , PRICE; ;
- , ;
- ,
19
PRICE, :
- ;
- , ;
- .
, :
- , PRICE; ;
- , , ;
- , .
20
ORDER, :
- ; - ;
- .
, :
- , ORDER; ;
- , , ;
- , .
21
STUDENT, ; - ;
- ;
- ( ). , :
- , STUDENT; ;
- , ;
22
STUDENT, ; - ;
- ;
- ( ). , :
- , STUDENT; ;
- , 2.0 4.5.
23
PRICE, :
- ;
- , ;
- .
, :
- , PRICE;
- , , ;
- ,
24
PRICE, :
- ;
- , ;
- .
, :
- , PRICE;
- , ;
- ,
25
COMP, :
- ;
- ;
- .
, :
- , COMP;
- ;
25
COMP, :
- ;
- ;
- .
, :
- , COMP;
- 64 1024.
26
COMP, :
- ;
- ;
- .
, :
- , COMP;
- 128 1000.
. .
f1.txt
f3.txt
Result:
................................3
.............................5
.................................2
...............................6
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct Tstud{
char FIO[90];
int data;
};
int main()
{
int i;
const int n=4;
cout<<""<<endl;
ifstream innn("f1.txt");//
Tstud PM[n];
char str[90];
//
for(i=0;i<n;i++)
{
innn.getline(PM[i].FIO,90);
innn.getline(str,90);
PM[i].data=atoi(str);
}//
//
ofstream binout("f2.txt",ios::binary); //
binout.write((char*)&PM[0],sizeof(Tstud)*n);
binout.close();
//
Tstud PM2[n];
//
ifstream bincin("f2.txt",ios::binary); //
for(i=0;i<n;i++)
{
bincin.read((char*)&PM2[i],sizeof(Tstud));
//bincin.read((char*)&PM2[0],sizeof(Tstud)*n);
}
bincin.close();
//
cout<<"Result: "<<endl;
for(i=0;i<n;i++)
{
cout<<resetiosflags(ios::right);
cout.setf(ios::left);
cout<<setw(20)<<setfill('.');// cout.width(20); cout.fill('.');
cout<<PM2[i].FIO;
cout.width(20);// cout<<setw(20);
cout.setf(ios::right);
cout<<PM2[i].data<<'\n';
}
ofstream out("f3.txt",ios::binary); //
for(i=0;i<n;i++)
{
out<<resetiosflags(ios::right);
out.setf(ios::left);
out<<setw(20)<<setfill('.');// cout.width(20); cout.fill('.');
out<<PM2[i].FIO;
out.width(20);// cout<<setw(20);
out.setf(ios::right);
out<<PM2[i].data<<'\n';
}
bincin.close();
cout<<endl;
cout<<""<<endl;
cin.get();
return 1;
}
3
3.2
, . , : , / , .
FILE* fopen(const char *filename, const char *mode)
filename , , , .
mode :
r .
w . .
a , ,
+ .
t ( CR-LF).
b ( ).
b t, _fmode, fcntl.h.
FILE , stdio.h. .
(fopen), (fdopen), (freopen).
int fclose (FILE *stream)
0 EOF .
int fcloseall() , : stdin, stdout, stdprn, stderr stdaux.
int fgetc(FILE *stream)
int fputc(int c, FILE *stream)
, EOF.
stdin
Int fgetchar(void)
stdout
Int fputchar(int c)
. xxx.bin yyy.bin.
ß
FILE *in = fopen ("c:\\xxx.bin", "rt");
FILE *out = fopen ("c:\\yyy.bin", "wt");
if (!in) return;
while (!feof(in))
fputc(fgetc(in), out);
fcloseall();
char *fgets(char *s, int n, FILE *stream)
, , . 0 .
, n-1 . '\n' . CR-LF.
char *fputs(char *s, FILE *stream)
EOF . .
. xxx.txt yyy.txt .
ß
const int n = 100;
char buf [n];
FILE *in = fopen("c:\\xxx.txt","r");
FILE *out = fopen("c:\\yyy.txt","w");
if (!in) return;
while (!feof(in)) {
fgets(buf, n, in);
fputs(buf, out);
}
fcloseall();
size_t fread(void *ptr, size_t size, size_t n, FILE *stream)
ptr - ,
size -
n -
, .
. size_t stdio.h unsigned int.
size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream)
ptr - , ;
size - ;
n - .
, .