您的位置:首页 > 编程语言 > C语言/C++

关于C++文件读写的一些东西

2005-08-29 10:58 615 查看
#include <fstream.h>
int main()
{
ofstream fout("test");//建立流对象
if(!fout)
{
cout<<"cannot open output file./n";
return 1;
}
fout<<10<<" "<<123.456<<"/"This is a text file/"/n";
fout.close();
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fstream.h>
int main()
{
ofstream fout("test2");
if(!fout)
{
cout<<"cannot open output file./n";
return 1;
}
fout<<"hello!/n";
fout<<100<<' '<<hex<<100<<endl;
fout.close();
ifstream fin("test2");
if(!fin)
{
cout<<"cannot open input file./n";
return 1;
}
char str[80];
int i;
fin>>str>>i;
cout<<str<<" "<<i<<endl;
fin.close();
return 0;
}
程序运行后,首先建立一个输出文件TEST2,并向它写如数据,HELLO!,100 64
关闭输出文件TEST2,再将它按输入模式打开,并将字符串“HELLO!”赋给STR,再将整数100赋给I,
最后在屏幕上显示HELLO 100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: