C++ 文件处理
时间:2010-11-19 来源:luozhiyong131
/**************
文本文件信息写入
***************/
#include <iostream.h>
#include <fstream.h>
void main()
{
ofstream outfile("d:\\a.txt",ios::out);
if(! outfile) cerr<<"打开文件错误\n";
else
{
outfile<<"姓名\t"<<"英语\t"<<"计算机\t"<<endl;
outfile<<"罗志勇\t"<<"85.5\t"<<"98\t"<<endl;
outfile<<"Lzy\t"<<"80\t"<<"93.5\t"<<endl;
outfile.close();
}
}
/**************
文本文件信息读出
***************/
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
void main()
{
char s[256];
fstream infile("d:\\a.txt",ios::in);
if(! infile)
{
cerr<<"打开文件错误\n";
abort();
}
infile.getline(s,256);
cout<<s<<endl;
while(! infile.eof()) //是否到文件结尾
{
infile.getline(s,256);
cout<<s<<endl;
}
infile.close();
}
/**************
文本文件拷贝
***************/
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
void main()
{
fstream infile("d:\\a.txt",ios::in);
if(! infile)
{
cerr<<"打开a文件错误\n";
abort();
}
fstream outfile("d:\\b.txt",ios::out);
if(! outfile)
{
cerr<<"打开b文件错误\n";
abort();
}
char ch;
while(infile.get(ch)); //没成功
outfile.put(ch);
infile.close();
outfile.close();
}