使用ifstream 和 ofstream 你必须注意加iostream头文件外还要加 fstream
来源:互联网 发布:猪八戒淘宝刷流量 编辑:程序博客网 时间:2024/05/21 09:20
为了打开一个文件供输入或输出,除了iostream头文件外,还必须包含头文件:
#include <fstream>
为了打开一个头文件,我们必须声明一个ofstream类型的对象:
ofstream outfile( “ name-of-file” );
为了测试是否已经成功地打开了一个文件,我们可以这样写:
if ( !outfile )
cerr << “Sorry! We were unable to open the file!\n” ;
类似地,为了打开一个文件供输入,我们必须声明一个ifstream类型的对象:
ifstream infile ( “name of file “ );
if ( ! infile )
cerr << “Sorry! We were unable to open the file!\n” ;
举例:
#include <fstream>
#include "iostream.h"
#include <string>
using std::string;
using std::ofstream;
using std::ifstream;
int main()
{
ofstream outfile( "out_file " );
ifstream infile( "in_file " );
if( !outfile ){
cerr << "error: unable to open output file!\n" ;
return -1;
}
if( !infile ){
cerr << "error: unable to open input file!\n" ;
return -1;
}
string word;
while ( infile >> word )
outfile <<word << ' ';
return 0;
}
运行,结果如下:
error: unable to open input file!
运行的结果告诉我们必须先建立一个input file, 另一方面也告诉我们声明了ofstream outfile( "out_file " )后,程序会自动建立文件out_file, 然后我们在且只能在源文件所在目录下(由于没有指定文件路径),新建in_file文件(注意:没有后缀)。
可以用记事本打开,并写入“hello world!”。保存,关闭后,再次运行程序,然后我们在源文件所在目录下,看到新建的output file。
使用记事本打开out_file, 文件里已被写入“hello world!"。
如果使用文本文档输入输出,则我们在声明ofstream, ifstream时,编写如下代码
ofstream outfile( "out_file.txt" );
ifstream infile( "in_file.txt" );
如果我们想改变文档所在的位置,则可以如下编写代码(将文件路径改为C盘根目录)
ofstream outfile( "c:\out_file.txt" );
ifstream infile( "c:\in_file.txt" );
例子二
#include <fstream>
#include "iostream.h"
#include <string>
using std::string;
using std::ofstream;
using std::ifstream;
int main()
{
ofstream outfile( "c:\out_file.txt" );
ifstream infile( "c:\in_file.txt" );
if( !outfile ){
cerr << "error: unable to open output file!\n" ;
return -1;
}
if( !infile ){
cerr << "error: unable to open input file!\n" ;
return -1;
}
string word;
while ( infile >> word )
outfile <<word << ' ';
return 0;
}
建立文本文件in_file.txt, 并写入“hello world!”
运行程序
打开out_file.txt
文件里已被写入“hello world!"。(王朝网络 wangchao.net.cn)
#include <fstream>
为了打开一个头文件,我们必须声明一个ofstream类型的对象:
ofstream outfile( “ name-of-file” );
为了测试是否已经成功地打开了一个文件,我们可以这样写:
if ( !outfile )
cerr << “Sorry! We were unable to open the file!\n” ;
类似地,为了打开一个文件供输入,我们必须声明一个ifstream类型的对象:
ifstream infile ( “name of file “ );
if ( ! infile )
cerr << “Sorry! We were unable to open the file!\n” ;
举例:
#include <fstream>
#include "iostream.h"
#include <string>
using std::string;
using std::ofstream;
using std::ifstream;
int main()
{
ofstream outfile( "out_file " );
ifstream infile( "in_file " );
if( !outfile ){
cerr << "error: unable to open output file!\n" ;
return -1;
}
if( !infile ){
cerr << "error: unable to open input file!\n" ;
return -1;
}
string word;
while ( infile >> word )
outfile <<word << ' ';
return 0;
}
运行,结果如下:
error: unable to open input file!
运行的结果告诉我们必须先建立一个input file, 另一方面也告诉我们声明了ofstream outfile( "out_file " )后,程序会自动建立文件out_file, 然后我们在且只能在源文件所在目录下(由于没有指定文件路径),新建in_file文件(注意:没有后缀)。
可以用记事本打开,并写入“hello world!”。保存,关闭后,再次运行程序,然后我们在源文件所在目录下,看到新建的output file。
使用记事本打开out_file, 文件里已被写入“hello world!"。
如果使用文本文档输入输出,则我们在声明ofstream, ifstream时,编写如下代码
ofstream outfile( "out_file.txt" );
ifstream infile( "in_file.txt" );
如果我们想改变文档所在的位置,则可以如下编写代码(将文件路径改为C盘根目录)
ofstream outfile( "c:\out_file.txt" );
ifstream infile( "c:\in_file.txt" );
例子二
#include <fstream>
#include "iostream.h"
#include <string>
using std::string;
using std::ofstream;
using std::ifstream;
int main()
{
ofstream outfile( "c:\out_file.txt" );
ifstream infile( "c:\in_file.txt" );
if( !outfile ){
cerr << "error: unable to open output file!\n" ;
return -1;
}
if( !infile ){
cerr << "error: unable to open input file!\n" ;
return -1;
}
string word;
while ( infile >> word )
outfile <<word << ' ';
return 0;
}
建立文本文件in_file.txt, 并写入“hello world!”
运行程序
打开out_file.txt
文件里已被写入“hello world!"。(王朝网络 wangchao.net.cn)
- 使用ifstream 和 ofstream 你必须注意加iostream头文件外还要加 fstream
- fstream(ifstream和ofstream)使用详解
- fstream ifstream ofstream的使用
- 使用fstream,ifstream,ofstream读写文件的一种情形
- 详解文件操作(ifstream、ofstream、fstream)
- C++文件操作详解(ifstream、ofstream、fstream
- C++文件读写详解(ofstream,ifstream,fstream)
- C++ 读写文件方法--fstream,ofstream,ifstream
- fstream ifstream ofstream分块读写文件
- C++文件读写详解(ofstream,ifstream,fstream
- C++文件流-fstream-ifstream-ofstream
- fstream、ifstream、ofstream创建新文件
- fstream ifstream ofstream
- fstream和ifstream详细用法,c++文件输入输出要注意!!
- C++fstream,ifstream和outstream读取文件最后一行注意
- C++文件读写详解(ofstream,ifstream,fstream)还有简单使用
- 考考你的基础知识:C++ 文件操作ofstream、ifstream使用
- C++语法基础--标准IO库--文件的输入和输出(fstream,ifstream,ofstream)
- 八大排序算法总结
- Android获取其他包的Context实例,然后调用它的方法
- ZOJ 1942
- MediaErrors.h定义了多媒体格式的错误码,如常见的-1007错误
- [020] Android模拟器访问本地Web应用
- 使用ifstream 和 ofstream 你必须注意加iostream头文件外还要加 fstream
- Java反射经典实例 Java Reflection Cookbook
- 把阿拉伯数字的金额转换为中文大写数字
- linux 多线程资料 虽然老 但是灰常不错
- 如何在oracle中导入导出dmp数据库文件
- 随机数产生问题整理
- 用虚拟串口
- 免费企业邮箱: Google app企业邮局的申请
- web service中文资料合集