转自于:http://www.vckbase.com/document/viewdoc/?id=1355
文件I/O库很多,如传统的unix中的read(),open();ANSI C的<stdio.h>,fopen,fread()等;MFC有自己的文件处理类。但是很多I/O库很难跨平台使用。
C++中提供了<fstream>,该库提供了高级的自动控制机制。其具体使用方法如下。
1、创建文件流
输入文件流ifstream支持重载的>>操作符,输出文件流ofstream支持重载的<<操作符。下面的程序创建一个ifstream对象 file,并将该对象中的每一个单字显示在屏幕上:
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
string s;
cout<<"enter file name:";
cin>>s;
ifstream f(s.c_str());
if(!f) exit(-1);
while(f>>s)
cout<<s<<"\n";
}
fstream对象只接受常量字符串作为文件名。我们不必显示检查EOF,因为重载操作符>>会自动处理。我们也不必显示关闭此文件,因为西沟函数会为我们做这件事。
关于文件的打开模式
如果不显示指定打开模式,fstream类将使用默认值,即ifstream默认以读方式打开文件并将文件指针置为文件的开头。ofstream则默认以写方式打开文件并将文件指针置为文件开头。
<fstream>定义的打开模式和文件属性:
ios::app //从后面添加
ios::ate //打开并找到文件尾
ios::binary //二进制模式打开
ios::in //只读打开
ios::out //写打开
ios::trunc //将文件截为0长度
可使用模式的组合。
如:fstream logfile("Database.dat",ios::in|ios::out);
2、设置文件的位置
可以调用seekp()函数,将文件指针指向该文件的某个偏移位置,该函数以字节为单位。tellp()可以报告新位置。
另可以用下面的常量重新定位文件。
ios::beg
ios::cur
ios::end
3、读写数据
如:fstream logfile("log.dat");
logfile<<teim(0)<<"danny"<<"\n";
logfile.seekp(ios::beg);
logfile>>login>>user;
分享到:
相关推荐
使用 <fstream> 类进行文件的 I/O 处理 摘要:传统的文件 I/O 库如 Unix 的 <io.h> 和 <stdio.h> ,由于其程序接口的原因,在很大程度上强制程序员进行某些处理,缺乏类型安全和国际化支持。C++ 的 <fstream> 库则在...
传统的文件 I/O 库如 Unix 的 <io.h> 和 <stdio.h> ,由于其程序接口的原因,在很大程度上...C++ 的 <fstream> 库则在文件的 I/O 方面提供了一个增强的、面向对象的、具有国际化意识的库。 希望本例对大家有所帮助!
C++ 标准库头文件大全 部分展示:合集可下载查看 通用实用程序 <algorithm>:算法 <array>:数组 <bitset>:位集 <chrono>:时间和日期 <complex>:复数 ...<fstream>:文件流 <iomanip>:输入/输出操作符格式化
<fstream> 支持文件的流输入输出 <iosfwd> 为输入输出对象提供向前的声明 <streambuf> 支持流输入和输出的缓存 <cstdio> 为标准流提供C样式的输入和输出 <cwchar> 支持多字节字符的C样式输入输出
纯C++标准库实现,仅用到了<fstream><map><list><string>,这些标准库,没有在Linux下测试过。(简易实现,代码部分仅6K,全采用string类型) /* .ini 文件 解析器【只能读取】 Section和Key只能为字母、数字、...
#include<fstream> using namespace std; const int maxsize=100; const int max=20; struct device //设备 { int code; //设备号 char name[20]; //设备名 int now_num; //现有库存 float buy_price; //价格 ...
#include <fstream.h> //文件输入/输出 #include <iomanip.h> //参数化输入/输出 #include <iostream.h> //数据流输入/输出 #include <limits.h> //定义各种数据类型最值常量 #include <locale.h> //定义...
<fstream> 文件的I/O库提供了ANSI C的<stdio.h>很多有用的优点,包括面向对象的界面,国际化和地方化的支持,安全性能,以及简单的调试
#include <fstream> using namespace std; int main () { int length; char * buffer; ifstream is; is.open ("test.txt", ios::binary ); // get length of file: is.seekg (0, ios::end); length = is....
cout<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"数学"<<setw(10)<<"外语"<<setw(10)<<"C++"<<setw(10)<<"总分"<<setw(10)<<"平均分"<<endl; while(p->next!=null) { cout<<p->num<<setw(10)<<p-...
使用zlib-1.2.11版本的开源库,实现我需要的对文件或者文件夹的压缩,查阅了一些博客大牛的资料,后面根据自己的需要修改。 下面给出我的代码: #include stdafx.h #include <string> #include <iostream> #include...
#include <fstream.h> //文件输入/输出 #include <iomanip.h> //参数化输入/输出 #include <iostream.h> //数据流输入/输出 #include <limits.h> //定义各种数据类型最值常量 #include <locale.h> //...
#include <fstream> #include <iostream> #include <nbt> #include <nbt> #include <nbt> int main() { // Write data to level.dat nbt::compound_node out_root; out_root.put("Greeting", std::make_shared...
#include <fstream> #include <map> #include <array> #include <assert> #include <string> #include <numeric> #include <algorithm> #include <vector> #include <math.h> #include <complex> 主要类型 ...
include<string> #include<iostream> #include<fstream> #include<cstdlib> #include<sstream> #include<conio.h> using namespace std; struct ProductNode { string NO;//型号 string Name;//名称
#include<fstream> #include<process> #include<graphics> #include<stdio> #include<conio> #include<dos> 特征 因此,此应用程序没有任何高级功能,但具有普通功能和图形标题的用法,以使普通UI变得最小,有效且...
# include < fstream> # include < iostream> # include < string> int main () { std::string const greeting{ u8" hello, 世界" }; assert ( greeting. length () == 13 ); // wrong length std::u16string ...
csv_library 一个简单的基于流或对象的csv解析和创建C ++库。 使用该库,您可以将CSV文件流式传输到对象中,或者将CSV行作为字符串添加到对象中,并...#include <fstream> #include <deque> #include <string> int mai
检测结果分行建立测试覆盖率更多信息开发: 主: 激励实例#0 # include < iostream># include < fstream># include < string># include " boost/pfr.hpp "struct some_person { std::string name; unsigned birth_...
最近做了个题目,遇到了要将字符串前后空格消除的细节问题。在Java中好像有一个字符串函数为trim()可以消除字符串后的... 代码如下:#include <iostream> #include <vector> #include <string> #include <fstream> usi