文件输入和输出流
2015-11-16 20:17
423 查看
在常用的三种流中处理文件的分别是:
iftream,ofstream,fstream;
ifstream:表示可以读取的文件流
ofstream:表示可以写入的文件输出流
fstream:表示可以进行读写操作的文件流
1、如何读取一个文件?
在c++中读取文件要使用iostream头文件,具体实现:
(1)必须包含头文件iostream
(2)头文件iostream定义可一个用于处理输入的iostraem类
(3)头文件中iostream声明了一个名为cin的istream变量
(4)必须指明命名空间std;
可以结合使用cin和操作符<<进行读取文件的各种数据类型,黑可以使用cin和get()读取一个字符。
使用cin和getline()读取一行字符。
<span style="font-size:18px;">#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
{
char ch;
fstream openfile("F:\\a.txt0", ios::out);//可以进行读写文件流
while (openfile.eof())
{
cout << "fileis not open" << endl;
exit(1);
}
while (openfile.eof())//判断是否到了结尾
{
openfile.get(ch);
cout << ch;
}
openfile.close();
}</span>
iftream,ofstream,fstream;
ifstream:表示可以读取的文件流
ofstream:表示可以写入的文件输出流
fstream:表示可以进行读写操作的文件流
1、如何读取一个文件?
在c++中读取文件要使用iostream头文件,具体实现:
(1)必须包含头文件iostream
(2)头文件iostream定义可一个用于处理输入的iostraem类
(3)头文件中iostream声明了一个名为cin的istream变量
(4)必须指明命名空间std;
可以结合使用cin和操作符<<进行读取文件的各种数据类型,黑可以使用cin和get()读取一个字符。
使用cin和getline()读取一行字符。
<span style="font-size:18px;">#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
{
char ch;
fstream openfile("F:\\a.txt0", ios::out);//可以进行读写文件流
while (openfile.eof())
{
cout << "fileis not open" << endl;
exit(1);
}
while (openfile.eof())//判断是否到了结尾
{
openfile.get(ch);
cout << ch;
}
openfile.close();
}</span>
#include<iostream> #include<fstream> #include<string> using namespace std; void main() { ofstream oftest; char filename[] = "E:\\a.txt";//进行写入操作。吧数据写到文件中 oftest.open(filename, ostream::app); oftest << a(); oftest.close(); } int a() { return 0; }
相关文章推荐
- web前端初步学习的自我总结
- centos更新镜像源问题
- [Android] 关于Android的.so文件你所需要知道的 - 简书
- hdu 4325 Flowers (区间处理 离散化)
- Ubuntu Server 14.04 配置VNC
- ImageNet Classification with Deep Convolutional Neural Network解读
- 20135326、20135303-linux实验一实验报告
- nginx args
- 使用Animations动画改变View的cornerRadius半径
- 你不知道的scanf和fscanf
- Struts2-----Ognl表达式语言
- 第十二周“知原理”检验题目第3题
- HDU 5523 Game
- Ubuntu命令
- [mysql]explain语句中的key_len计算
- 笔记七(随机运动)
- java中为什么要实现序列化,什么时候实现序列化?
- window.location的相关用法
- android中的Application
- UI根视图控制器