/ << >>.
:
#include <iostream.h>
#include <fstream.h>
Void main(void)
{
ofstream out(myfile.txt);
if (!out)
{
cout<< \n; return 1;
}
out<<10<< <<10<< <<2004;
out<< ;
Out.close();
}
:
#include <iostream.h>
#include <fstream.h>
Void main(void)
{
Char ch;
Int i;
Float f;
char str[80];
ifstream in(myfile.txt);
if (!in)
{ cout<< \n; return 1; }
in>>i; in>>f; in>>ch; in>>str;
cout<<i<< <<f<< <<ch<<endl;
cout<<str<<endl;
In.close();
}
getline() iostream.h
char *getline(char *s,int n);
s - () ; n - .
, .
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<stdio.h>
Void main(void)
{
ofstream fcout("aaa.cpp");
Ifstream fcin;
char *tmp=new char[80];
char *fname=new char[20];
char *word=new char[80];
char *ptr;
int a=0;
cout<<"Give name file:"<<endl;
Do
{
If(a)
{
cout<<"Invalid name file!! Give correct name:"<<endl;
}
Cin.getline(fname,20);
Fcin.open(fname);
a=1;
} while(!fcin);
cout<<"\give word:";
Cin.getline(word,80);
while(!fcin.eof()) /* read line from file */
{
Fcin.getline(tmp,80);
if ((ptr=strstr(tmp,word))!=NULL)
{
cout<<tmp<<endl;
fcout<<tmp<<endl;
}
}
Fcout.close();
Fcin.close();
Getch();
}
/.
. - get() put(). :
ifstream &get(char &ch);
ofstream &put(char ch);
get() ch. put() ch . .
#include <iostream.h>
#include <fstream.h>
Void main(void)
{
Char ch;
Ifstream in;
in.open(myfile.bin,ios::binary,1);
if(!in)
{
cout<<\n );
Return 1;
}
while(in.get(&ch))
cout<<ch;
In.close();
}
in , in
|
|
#include <iostream.h>
#include <fstream.h>
Oid main(void)
{
char *p=Hello, World!;
ofstream out(test);
if(!out)
{
cout<< \n;
Return 1;
}
while (*p)
out.put(*p++);
Out.close();
}
read(), write() :
ifstream &read(unsigned char *buf, int num); ofstream &write(unsigned char *buf, int num);
read() num buf. write() num buf.
#include <iostream.h>
#include <fstream.h>
Void main(void)
{
float numbers[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
Int i;
ofstream out(test);
if (!out)
{
cout<< \n;
Return 1;
}
out.write((unsigned char *) numbers, sizeof numbers);
Out.close();
for(i=0;i<10;i++)
numbers[i]=0;
ifstream in(test);
if (!in)
{
cout<< \n;
Return 1;
}
in.read((unsigned char *) numbers, sizeof numbers);
for(i=0;i<10;i++)
cout<<numbers[i]<< ;
cout<<\n;
cout<< <<in.gcount()<< \n;
In.close();
}
gcount() read().
eof(), , .
/ ++, , (streams). 4 : cin, cout, cerr, clog. cin , cout . cerr clog . cerr , . . . clog , , . . cin , cout , .
. << - inserting ( ), >> - extracting ( ).
.
#include <iostream.h>
Void main(void)
{
char name[100];
cout<<\n , :;
cin>>name;
cout>>\n ,;
cout<<name;
}
++ : -, .
-.
ios. : width(), fill(), precision().
width
int width() .
int width(int) .
:
- ;
|
|
- ;
- , fill;
- , width ;
- width 0;
- width .
#include <iostream.h>
const int MAX_LEN = 10; const int FLD_WIDTH = 10;
Void main(void)
{
char name[MAX_LEN];
int x=2867; int y=20051; int z=017;
cout<<|n Give name <<(max <<MAX_LEN-1<< symbols): ;
Cin.width(MAX_LEN);
cin>>name;
cout<<\n Hello, <<name;
Cout.width(FLD_WIDTH);
cout<<x<<\n;
Cout.width(FLD_WIDTH);
cout<<y<<\n;
Cout.width(FLD_WIDTH);
cout<<z<<\n;
}
fill
int fill() ;
int fill(char) .
.
#include <iostream.h>
const int FLD_WIDTH = 10; const int FILL_CHAR = 0;
Void main(void)
{
int x=2867, y=20051;
Cout.fill(FILL_CHAR);
Cout.width(FLD_WIDTH);
cout<<x<<\n;
Cout.width(FLD_WIDTH);
cout<<y<<\n;
}
precision
int precision() ;
int precision(int) . :
- 6 ;
- scientific fixed, precision ;
- scientific fixed, precision ;
precision
#include <iostream.h>
Void main(void)
{
float f = 3456.141592;
double d = 50.2345639101;
Cout.precision(4);
cout<<d<<\n; // 50.23
cout<<f<<\n; // 3456
Cout.precision(3);
cout<<f<<\n; // 3.46e+3
}
.
++ . , . , long. , ios .
long ios::flags() | |
long ios::flags(long) | , , |
long ios::setf(long,long) | , , . . |
long ios::setf(long) | , ; . |
long ios::unsetf(long) | , ; . |
ios::skipws | X | , |
ios::left | , . | |
ios::right | X | ios::left ios::internal, . |
ios::internal | , , . fill | |
ios::dec | X | , 10 ( ) |
ios::oct | , 8( ) | |
ios::hex | , 16( ) | |
ios::showbase | , (0x 16- 0 ). | |
ios::showpoint | , float, double, long double . | |
ios::uppercase | , A F 16- . | |
ios::showpos | , + | |
ios::scientific | , (.. n.xxxEy, 1.234e2) | |
ios::fixed | , (.. nnn.ddd, 123.45) |
, .
|
|
#include <iostream.h>
#include <limits.h>
Void main(void)
{
int i=0;
cout<< : ;
//
cin>>i;
//
cout<< << i << endl;
//
cin.ignore(INT_MAX, \n);
cout<< : ;
// skipws
cin.unsetf(ios::skipws);
//
cin >> i;
}
#include <iostream.h>
Void main(void)
{
int x = 1678;
//
cout<< x= <<x<<\n;
//
long savedFlags = cout.flags;
// 16
cout.setf(ios::showbase|ios::hex);
//
cout<< x=<< x <<\n;
}
#include <iostream.h>
Void main(void)
{
float f = 2.3456789e6;
double d = 3.0e9;
cout << f = << f <<\n;
cout << d = << d <<\n;
// +
cout.setf(ios::showpos);
//
cout << f = << f <<\n;
cout << d = << d <<\n;
}
.
, . .
, , . .
endl | (\n) flush |
ends | (\0). . |
flush | . |
dec | 10 (. ios::dec) |
hex | 16 (. ios::hex) |
oct | 8 (. ios::oct) |
ws | (. ios::skipws) |
#include <iostream.h>
#include <iomanip.h>
Void main(void)
{
Int i;
cout << :; cin >> i;
if (!cin) cout << .<<endl;
Else
{
// hex, oct, dec
cout<<Hex: << hex << i << endl;
cout<<Oct: << oct << i << endl;
cout<<Dec: << dec << i << endl;
}
}