📄 练习.txt
字号:
19.1
# include <iostream.h>
# include <fstream.h>
void main()
{
ifstream fin("e:\\c++\\test.txt",ios::nocreate);
if(fin.fail())
{
cerr<<"error open files!"<<endl;
return;
}
int i=0;
char letter;
letter=fin.get();
while (!fin.eof())
{
if(letter=='\n')
{
//cout<<"Enter be encounted."<<endl;
i++;
letter=fin.get();
}
else
{
letter=fin.get();
}
}
i++;
cout<<"lines:"<<i<<endl;
}
19.3
#include <iostream.h>
#include <strstrea.h>
#include <iomanip.h>
void main()
{
char* string="1 2 3 4 5 6 7 8 9";
int i;
istrstream inf(string,0);
while(!inf.eof())
{
inf>>i;
cout<<setw(2)<<i;
}
cout<<endl<<"end of program."<<endl;
}
19.4
#include <iostream.h>
#include <iomanip.h>
void main()
{
char ch='A';
for(;ch<='Z';ch++)
{
cout<<ch<<":";
cout<<"0x"<<hex<<setiosflags(ios::uppercase)<<(int)ch<<endl;
}
cout<<"end of program."<<endl;
}
19.5
//可以通过编译,但是运行时出错。
# include <fstream.h>
# include <iostream.h>
class person
{
public:
person();
person(char * a,char * b=0,char* c=0,char* d=0,char * e=0)
{
name=a;
dep=b;
tel1=c;
street=d;
tel2=e;
}
void display(ostream & out)
{
out<<"Name:"<<name<<"Dept:"<<dep<<"tel of office:"<<tel1
<<"address:"<<street<<"tel of home:"<<tel2<<endl;
}
protected:
char * name;
char * dep;
char * tel1;
char * street;
char * tel2;
};
ostream & operator<<(ostream & m,person & n)
{
n.display(m);
return m;
}
void main()
{
fstream fdev("e:\\c++\\test2.txt",ios::in|ios::out|ios::ate,filebuf::sh_none);
char * j=0;
char * k=0;
char * x=0;
char * y=0;
char * z=0;
cin>>j>>k>>x>>y>>z;
cout<<j<<k<<x<<y<<z;
while(j!=0)
{
fdev<<j<<" "<<k<<" "<<x<<" "<<y<<" "<<z<<"/n";
cin>>j>>k>>x>>y>>z;
}
cout<<endl;
//ifstream fd("e:\\c++\\test2.txt");
while(!fdev.eof())
{
char * a=0;
char * b=0;
char * c=0;
char * d=0;
char * e=0;
fdev>>a>>b>>c>>d>>e;
person p=person(a,b,c,d,e);
cout<<p;
}
cout<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -