📄 io流.txt
字号:
//**************************
//** ch19_1.cpp **
//**************************
#include <iostream.h>
void fn(int a,int b)
{
if(b==0)
cerr<<"zero encountered."
<<"The message cannot be redirected."<<endl;
else
cout<<a/b<<endl;
}
void main()
{
fn(20,2);
fn(20,0);
}
//**************************
//** ch19_2.cpp **
//**************************
#include <fstream.h>
void fn()
{
ofstream myf("e:\\c++\\myname.txt");
myf<<"In each of the following questions, a related pair"<<endl
<<"of words or phrases is followed by five lettered pairs"<<endl
<<"of words or phrases."<<endl;
}
void main()
{
fn();
}
//**************************
//** ch19_3.cpp **
//**************************
#include <iostream.h>
#include <strstrea.h>
char* parseString(char* pString)
{
istrstream inp(pString,0);
int aNumber;
float balance;
inp>>aNumber>>balance;
char* pBuffer = new char[128];
ostrstream outp(pBuffer,128);
outp<<"a Number = "<<aNumber
<<", balance = "<<balance;
return pBuffer;
}
void main()
{
char* str="1234 100.35";
char* pBuf=parseString(str);
cout<<pBuf<<endl;
delete []pBuf;
}
//**************************
//** ch19_4.cpp **
//**************************
#include <iostream.h>
void fn(float interest,float amount)
{
cout<<"RMB amount = ";
cout.precision(2);
cout<<amount;
cout<<endl<<"the interest = ";
cout.precision(4);
cout<<interest<<endl;
}
void main()
{
float f1=29.45167894;
float f2=12.5645678;
fn(f1,f2);
}
//**************************
//** ch19_5.cpp **
//**************************
#include <iostream.h>
#include <iomanip.h>
void fn(float interest,float amount)
{
cout<<"RMB amount = ";
cout<<setprecision(2)<<amount;
cout<<endl<<"the interest = "
<<setprecision(4)<<interest<<endl;
}
void main()
{
float f1=29.45167894;
float f2=12.5645678;
fn(f1,f2);
}
//**************************
//** ch19_6.cpp **
//**************************
#include <iostream.h>
#include <iomanip.h>
void main()
{
cout.width(8);
cout<<10
<<20<<endl;
}
//**************************
//** ch19_9.cpp **
//**************************
#include <iostream.h>
#include <iomanip.h>
void main()
{
float value = 2.345678;
int prePrecision;
prePrecision=cout.precision(4);
cout<<value<<endl;
cout.precision(prePrecision);
cout<<value<<endl;
cout<<prePrecision<<endl;
}
//***************************
//** ch19_10.cpp **
//***************************
#include <iostream.h>
void main()
{
char str[128];
cout<<"Type in a line of text and press Enter"<<endl;
cin.getline(str,sizeof(str),'X');
cout<<"First line: "<<str<<endl;
cin.getline(str,sizeof(str));
cout<<"Second line: "<<str<<endl;
}
//***************************
//** ch19_11.cpp **
//***************************
#include <iostream.h>
#include <ctype.h>
void main()
{
char letter;
while(!cin.eof())
{
letter=cin.get();
letter=toupper(letter);
if(letter=='Y')
{
cout<<"'Y' be met."<<endl;
break;
}
cout<<letter;
}
}
//***************************
//** ch19_13.cpp **
//***************************
//打开文件的操作
#include <fstream.h>
#include <iostream.h>
void main(int argc, char** argv)
{
ifstream in(argv[1]);
if(in.fail())
{
cerr<<"Error opening the file: "<<argv[1]<<endl;
return;
}
while(!in.eof())
cout<<(char)(in.get());
in.close();
}
//***************************
//** ch19_14.cpp **
//***************************
#include <iostream.h>
//#include <fstream.h>
#include <iomanip.h>
class RMB
{
public:
RMB(double v=0.0)
{
yuan=v;
jf=(v-yuan)*100.0+0.5;
}
operator double()
{
return yuan+jf/100.0;
}
void display(ostream& out)
{
out<<yuan<<"."
<<setfill('0')<<setw(2)<<jf
<<setfill(' ');
}
protected:
unsigned int yuan;
unsigned int jf;
};
ostream& operator << (ostream& oo, RMB& d)
{
d.display(oo);
return oo;
}
void main()
{
RMB rmb(1.5);
cout<<"Initially rmb = "<<rmb<<endl;
rmb=2.1 * rmb;
cout<<"then rmb = "<<rmb<<endl;
ofstream out("ab.txt");
out<<rmb;
//ofstream out("e:\\c++\\test.txt"); //注意路径的写法
//out<<rmb;
}
//****************************
//** ch19_140.cpp **
//****************************、
//文件及流操作练习
#include <fstream.h>
#include <iostream.h>
//#include <ostream.h>
#include <iomanip.h>
class Currency
{
public:
Currency(double v=0.0)
{
yuan=v;
jf=(v-yuan)*100.0+0.5;
}
virtual void display(ostream& out)=0;
protected:
unsigned int yuan;
unsigned int jf;
};
class RMB:public Currency
{
public:
RMB(double v=0.0):Currency(v){}
virtual void display(ostream& out)
{
out<<yuan<<"."
<<setfill('0')<<setw(2)<<jf<<setfill(' ')
<<" RMB";
}
};
class Dmark: public Currency
{
public:
Dmark(double v=0.0):Currency(v){};
virtual void display(ostream& out)
{
out<<yuan<<"."
<<setfill('0')<<setw(2)<<jf<<setfill(' ')
<<" DM";
}
};
class WHQ: public RMB
{
public:
WHQ(double v=0.0): RMB(v) {}
virtual void display(ostream& out)
{
RMB::display(out);
out<<" WHQ";
}
};
ostream& operator << (ostream& oo,Currency &c)
{
c.display(oo);
oo<<endl;
return oo;
}
void fn(Currency &c)
{
cout<<"Deposit is "<<c<<endl;
}
void main()
{
RMB rmb(5.5);
fn(rmb);
WHQ whq(5.6);
fn(whq);
Dmark mark(10.6);
fn(mark);
ofstream out("test.txt");
out<<rmb<<whq<<mark;
}
//--------ch19_15.prj--------//
//---------------------------//
//************************
//** main.cpp **
//************************
#include <fstream.h>
#include "student.h"
#include "master.h"
void main()
{
ofstream out("e:\\c++\\ch19\\master.txt");
Student s1("Dill Arnson",12567,3.5);
Master s2("Welchi Shammas",12667,4.1,'A');
Master s3("Portel Braumbel",12579,3.8,'B');
out<<s1;
out<<s2;
out<<s3;
}
//*************************
//** student.h **
//*************************
#include <iostream.h>
#include <string.h>
#ifndef STUDENT
#define STUDENT
class Student
{
public:
Student(char* pS,unsigned num,float g)
{
strcpy(pName,pS);
uID=num;
grade=g;
}
virtual void display(ostream& out);
protected:
char pName[20];
unsigned int uID;
float grade;
};
ostream& operator << (ostream& out,Student& st);
#endif
//***************************
//** student.cpp **
//***************************
#include "student.h"
#include <iomanip.h>
#include <iostream.h>
void Student::display(ostream& out)
{
out<<setiosflags(ios::left)<<setw(20)<<pName
<<uID<<","
<<setiosflags(ios::right)<<setw(4)<<grade;
}
ostream& operator << (ostream& out,Student& st)
{
st.display(out);
out<<endl;
return out;
}
//************************
//** master.h **
//************************
#include "student.h"
#include <iostream.h>
class Master:public Student
{
public:
Master(char* pS,unsigned num,float g,char t):
Student(pS,num,g),type(t) {}
void display(ostream& out);
protected:
char type;
};
//**************************
//** master.cpp **
//**************************
#include <iostream.h>
#include "master.h"
void Master::display(ostream& out)
{
Student::display(out);
out<<","<<type;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -