16-1
#include <iostream.h>
#include <strstrea.h>
void main()
{
char buf[]="12345678";
int i,j;
istrstream s1(buf);
s1 >> i;//将字符串转换为数字
istrstream s2(buf,3);
s2 >> j; //将字符串转换为数字
cout << i+j <<endl;//两个数字相加
}
16-2
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream ofile;
cout << "Create file1" << endl;
ofile.open("test.txt");
if(!ofile.fail())
{
ofile << "name1" << " ";
ofile << "sex1" << " ";
ofile << "age1";
ofile.close();
cout << "Create file2" <<endl;
ofile.open("test2.txt");
if(!ofile.fail())
{
ofile << "name2" << " ";
ofile << "sex2" << " ";
ofile << "age2";
ofile.close();
}
}
return 0;
}
16-3
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buf[128];
ofstream ofile("test.txt");
for(int i=0;i<5;i++)
{
memset(buf,0,128);
cin >> buf;
ofile << buf;
}
ofile.close();
ifstream ifile("test.txt");
while(!ifile.eof())
{
char ch;
ifile.get(ch);
if(!ifile.eof())
cout << ch;
}
cout << endl;
ifile.close();
return 0;
}
16-4
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("test.txt",ios::out);
if(!file.fail())
{
cout << "start write " << endl;
file << "name" << " ";
file << "sex" << " ";
file << "age" << endl;
}
else
cout << "can not open" << endl;
file.close();
return 0;
}
16-5
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("test.txt",ios::in);
if(!file.fail())
{
while(!file.eof())
{
char buf[128];
file.getline(buf,128);
if(file.tellg()>0)
{
cout << buf;
cout << endl;
}
}
}
else
cout << "can not open" << endl;;
file.close();
return 0;
}
16-6
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buf[50];
fstream file;
file.open("test.dat",ios::binary|ios::out);
for(int i=0;i<2;i++)
{
memset(buf,0,50);
cin >> buf;
file.write(buf,50);
file << endl;
}
file.close();
file.open("test.dat",ios::binary|ios::in);
while(!file.eof())
{
memset(buf,0,50);
file.read(buf,50);
if(file.tellg()>0)
cout << buf;
}
cout << endl;
file.close();
return 0;
}
16-7
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
char name[20];
char c;
cout<<"请输入文件:"<<"\n";
cin>>name;
infile.open(name);
if(!infile)
{
cout<<"文件打开失败!";
exit(1);
}
strcat(name,"复本");
cout<< "start copy" << endl;
outfile.open(name);
if(!outfile)
{
cout<<"无法复制";
exit(1);
}
while(infile.get(c))
{
outfile << c;
}
cout<<"start end"<< endl;
infile.close();
outfile.close();
return 0;
}
16-8
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream ofile("test.txt", ios::app);
if(!ofile.fail())
{
cout << "start write " << endl;
ofile << "Mary ";
ofile << "girl ";
ofile << "20 ";
}
else
cout << "can not open";
return 0;
}
16-9
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifile("test.txt");
if(!ifile.fail())
{
while(!ifile.eof())
{
char ch;
streampos sp = ifile.tellg();
ifile.get(ch);
if(ch == ' ' )
{
cout << "postion:" << sp ;
cout <<"is blank "<< endl;
}
}
}
return 0;
}
16-10
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifile;
char cFileSelect[20];
cout << "input filename:";
cin >> cFileSelect;
ifile.open(cFileSelect);
if(!ifile)
{
cout << cFileSelect << "can not open" << endl;
return 0;
}
ifile.seekg(0,ios::end);
int maxpos=ifile.tellg();
int pos;
cout << "Position:";
cin >> pos;
if(pos > maxpos)
{
cout << "is over file lenght" << endl;
}
else
{
char ch;
ifile.seekg(pos);
ifile.get(ch);
cout << ch <<endl;
}
ifile.close();
return 1;
}
16-11
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const char* filename="test.txt";
fstream iofile;
iofile.open(filename,ios::in);
if(iofile.fail())
{
iofile.clear();
iofile.open(filename, ios::in| ios::out| ios::trunc);
}
else
{
iofile.close();
iofile.open(filename, ios::in| ios::out| ios::ate);
}
if(!iofile.fail())
{
iofile << "我是新加入的";
iofile.seekg(0);
while(!iofile.eof())
{
char ch;
iofile.get(ch);
if(!iofile.eof())
cout << ch;
}
cout << endl;
}
return 0;
}
16-12
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char file[50];
cout <<"Input file name:"<<"\n";
cin >>file;
if(!remove(file))
{
cout <<"The file:"<<file<<"已删除"<<"\n";
}
else
{
cout <<"The file:"<<file<<"删除失败"<<"\n";
}
}
16-13
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
char namesrc[20];
char namedes[20];
char c;
cout<<"请输入含有字母的文件:"<<"\n";
cin>>namesrc;
infile.open(namesrc);
if(!infile)
{
cout<<"文件打开失败!";
exit(1);
}
cout<<"请输入目标文件:"<<"\n";
cin >> namedes;
outfile.open(namedes,ios::app);
if(!outfile)
{
cout<<"文件打开失败!";
exit(1);
}
while(infile.get(c))
{
if(c>'A' || c<'z')
outfile << c;
}
cout<<"复制完成"<< endl;
infile.close();
outfile.close();
return 0;
}
16-14
#include <iostream >
#include <time.h>
#include <fstream>
using namespace std;
void main ()
{
char name[128];
char password[128];
char timebuf[256];
char body[50];
cout << "请输入用户名" << endl;
cin >> name;
cout << "请输入密码" << endl;
cin >> password;
cout << "登录成功,登录记录已写入日志" << endl;
fstream file;
file.open("test.dat",ios::binary|ios::out);
time_t tCurrentTime;
tCurrentTime = time ( ( time_t* ) NULL );
strftime ( timebuf, sizeof ( timebuf ), "%H:%M:%S %y-%m-%d ", localtime ( &tCurrentTime ) );
file.write(timebuf,256);file.write(name,128);
file.write(name,128);
strcpy(body,"登录.");
file.write(body,50);
file.close();
}