频道栏目
首页 > 资讯 > C++ > 正文

C/C++ 初学简单笔记 —5— 文件I/O

14-01-25        来源:[db:作者]  
收藏   我要投稿
#include
#include
#include


using namespace std;


/*******************************
文件流处理方式的枚举元素
ios::app 追加文件
*******************************/


// 信息写出到文件中
int Mywrite()
{
ofstream fout;
fout.open("D:\\test.txt",ios::app);
//fout.open("CON"); // 定向到显示屏
if (!fout)
{
cout<<"file open failed"<<>
return -1;
}
fout<<"No problem"<<>
cout<<"No problem"<<>
fout.close();
return 1;
}


// 将文件中的内容读取到程序中
int Myread()
{
ifstream fin;
char str[50];
fin.open("D:\\test.txt");


if (!fin)
{
cout<<"file read failed"<<>
return -1;
}


fin.getline(str,50);
cout<<><>


fin.close();
return 1;


}


// 二进制流读写
int bwrite()
{
ofstream fout;
fout.open("D:\\b.txt",ios::in|ios::binary);
if (!fout)
{
cout<<"file open failed"<<>
return -1;
}
char str[]="Evin_____________";
fout.write(str,strlen(str));
cout<<><>
fout.close();
return 1;
}


int bread()
{
ifstream fin;
char str[50];
char str2[50];


fin.open("d:\\b.txt",ios::binary);
if (!fin)
{
cout<<"file read failed"<<>
return -1;
}


fin.read(str,50);
cout<<><>
fin.close();
return 1;
}
int main()
{
bwrite();
Myread();
//bread();
return 0;
}


总结
1. 要引入头文件
#include
2. 打开文件, 检查是否正确打开
ofstream fout;
fout.open("D:\\text.txt");
if (!fout) {...}
3. 读/写 信息
fout<<><>
fin.getline(info,strlen);
4. 关闭文件
fout.close()/ fin.close()



注:
1. CON 可重定向到屏幕
如: fout.open("CON"); // 将显示到屏幕
2. 有一些文件处理的方式
如: ios::app 表示追加文件

fout.open("D:\\text.txt",ios::app); // 表示存入的信息追加到后面。



相关TAG标签
上一篇:1)Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example: 1)- add -D_WIN32_WINNT=0x
下一篇:iOS 基础集合类
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站