★文件的打开与关闭:
需在头文件中包含fstream,文件的操作对象为ifstream、 ofstream类对象,需要先定义ifstream/ofstream类对象,然后建立对象与文件的关联,并指定工作模式,再调用成员函数open()/close(),实现文件的打开和关闭。
如:ofstream outfile("f1.dat",ios::out);
ofsteeam outfile; outfile.open("f1.dat",ios::out);
★文件的输入与输出:
要用“<<”“>>”或文件流的成员函数(put,get,getline)来进行文件的输入与输出。
例1:从键盘输入10个整数放入一个数组中并输出到文件f1.dat中,再将f1.dat的数据读入另一个数组并在屏幕输出,并输出其中最大的数和它所在数组中的序号。
#include<fstream>
using namespace std;
void compare(int a[],int n);
int main()
{
int a[10];
for(int i=0;i<10;i++)
cin>>a[i];
ofstream outfile("f:/f1.txt",ios::out);
if(!outfile)
{
cerr<<"open file error!"<<endl;
exit(1);
}
for(int i=0;i<10;i++)
outfile<<a[i]<<" ";
outfile.close();
int b[10];
ifstream infile("f:/f1.txt",ios::in);
if(!infile)
{
cerr<<"open file error!"<<endl;
exit(1);
}
for(int i=0;i<10;i++)
{
infile>>b[i];
cout<<b[i]<<" ";
}
cout<<endl;
compare(b,10);
infile.close();
return 0;
}
void compare (int a[],int n)
{
int max=a[0],t=0;
for(int i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
t=i;
}
}
cout<<"max=a["<<t<<"]"<<": "<<max<<endl;
}
例2:从键盘输入一行字符,将其中的字母部分输出到文件f1.txt中,再将其读入到程序中,将其中的小写部分转换为大写字母输出到屏幕上,并存入到f1.txt文件的后面。
#include<fstream>
using namespace std;
int main()
{
char p[100];
ofstream outfile("f:/f1.txt",ios::out);
if(!outfile)
{
cerr<<"open file error!"<<endl;
abort();
}
cin.get(p,100);
for(int i=0;p[i]!='\0';i++)
{
if((p[i]>=65 && p[i]<=90) || (p[i]>=97 && p[i]<=122))
outfile<<p[i];
}
outfile.close();
ifstream infile("f:/f1.txt",ios::in);
if(!infile)
{
cerr<<"open file error!"<<endl;
abort();
}
char ch[100];
while(!infile.eof())
{
infile>>ch;
}
for(int i=0;ch[i]!='\0';i++)
if(ch[i]>=97 && ch[i]<=122)
ch[i]-=32;
cout<<ch<<endl;
infile.close();
ofstream fout("f:/f1.txt",ios::out|ios::app);
fout<<ch<<endl;
fout.close();
return 0;
}
需在头文件中包含fstream,文件的操作对象为ifstream、 ofstream类对象,需要先定义ifstream/ofstream类对象,然后建立对象与文件的关联,并指定工作模式,再调用成员函数open()/close(),实现文件的打开和关闭。
如:ofstream outfile("f1.dat",ios::out);
ofsteeam outfile; outfile.open("f1.dat",ios::out);
★文件的输入与输出:
要用“<<”“>>”或文件流的成员函数(put,get,getline)来进行文件的输入与输出。
例1:从键盘输入10个整数放入一个数组中并输出到文件f1.dat中,再将f1.dat的数据读入另一个数组并在屏幕输出,并输出其中最大的数和它所在数组中的序号。
#include<fstream>
using namespace std;
void compare(int a[],int n);
int main()
{
int a[10];
for(int i=0;i<10;i++)
cin>>a[i];
ofstream outfile("f:/f1.txt",ios::out);
if(!outfile)
{
cerr<<"open file error!"<<endl;
exit(1);
}
for(int i=0;i<10;i++)
outfile<<a[i]<<" ";
outfile.close();
int b[10];
ifstream infile("f:/f1.txt",ios::in);
if(!infile)
{
cerr<<"open file error!"<<endl;
exit(1);
}
for(int i=0;i<10;i++)
{
infile>>b[i];
cout<<b[i]<<" ";
}
cout<<endl;
compare(b,10);
infile.close();
return 0;
}
void compare (int a[],int n)
{
int max=a[0],t=0;
for(int i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
t=i;
}
}
cout<<"max=a["<<t<<"]"<<": "<<max<<endl;
}
例2:从键盘输入一行字符,将其中的字母部分输出到文件f1.txt中,再将其读入到程序中,将其中的小写部分转换为大写字母输出到屏幕上,并存入到f1.txt文件的后面。
#include<fstream>
using namespace std;
int main()
{
char p[100];
ofstream outfile("f:/f1.txt",ios::out);
if(!outfile)
{
cerr<<"open file error!"<<endl;
abort();
}
cin.get(p,100);
for(int i=0;p[i]!='\0';i++)
{
if((p[i]>=65 && p[i]<=90) || (p[i]>=97 && p[i]<=122))
outfile<<p[i];
}
outfile.close();
ifstream infile("f:/f1.txt",ios::in);
if(!infile)
{
cerr<<"open file error!"<<endl;
abort();
}
char ch[100];
while(!infile.eof())
{
infile>>ch;
}
for(int i=0;ch[i]!='\0';i++)
if(ch[i]>=97 && ch[i]<=122)
ch[i]-=32;
cout<<ch<<endl;
infile.close();
ofstream fout("f:/f1.txt",ios::out|ios::app);
fout<<ch<<endl;
fout.close();
return 0;
}