首页 / C++ / C++学习笔记——文件操作
C++学习笔记——文件操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++学习笔记——文件操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3226字,纯文字阅读大概需要5分钟。
内容图文
![C++学习笔记——文件操作](/upload/InfoBanner/zyjiaocheng/641/4f1dfe9fee134ed7bd04ba1c4e85553c.jpg)
C++在程序运行的时候产生的数据属于临时数据,在程序运行结束后释放掉,有时候我们需要把一些简单的数据永久的存储起来,这个时候就可以利用文件将数据持久化。
文件可以分为两种类型:
1.文本文件:文件以文本的ASCII码形式存储在计算机中;
2.二进制文件:文件以文本的二进制形式存储在计算机中,用户一般情况下不能读懂。
文件操作的三个类:
1.ofstream:写操作;
2.ifstream:读操作;
3.fstream:读写操作。
文件的打开方式:
打开方式 | 注释 |
ios::in | 打开文件,读文件 |
ios::out | 打开文件,写文件 |
ios::ate | 初始位置:文件尾 |
ios::app | 写文件,追加方式 |
ios::trunc | 如果文件存在,先删除,再创建 |
ios::binary | 二进制方式 |
文件打开方式可以利用 | 操作符配合使用。
文本文件的写操作流程:
#include <iostream>
#include <string>
//1.包含头文件#include <fstream>
#include <fstream>
using namespace std;
void test01(){
// 2.创建流对象
ofstream ofstream1;
// 3.打开文件,参数分别为文件路径和打开方式
ofstream1.open("test.txt",ios::out);
// 4.写数据
ofstream1<<"name:li"<<endl;
ofstream1<<"age:15"<<endl;
ofstream1<<"height:178cm"<<endl;
// 5.关闭文件
ofstream1.close();
}
int main() {
test01();
return 0;
}
文本文件的读操作流程:
#include <iostream>
#include <string>
//1.包含头文件#include <fstream>
#include <fstream>
using namespace std;
void test01(){
// 2.创建流对象
ifstream ifstream1;
// 3.打开文件,参数分别为文件路径和打开方式
ifstream1.open("test.txt",ios::in);
// 4.判断文件是否打开成功
if(!ifstream1.is_open()){
cout<<"文件打开失败"<<endl;
return;
}
// 5.读文件的第一种方式
string buffer;
while(getline(ifstream1,buffer)){
cout<<buffer<<endl;
}
// 读完文件后流状态为eof,所以要将流状态设置为goodbit,这样才能移动文件指针
ifstream1.clear(std::ios::goodbit);
// 移动文件指针到开始,读文件使用seekg,写文件使用seekp。
ifstream1.seekg(std::ios::beg);
// 读文件的第二种方式
char buffer1[1024]={0};
while(ifstream1>>buffer1){
cout<<buffer1<<endl;
}
ifstream1.clear(std::ios::goodbit);
ifstream1.seekg(std::ios::beg);
// 读文件的第三种方式
char buffer2[1024]={0};
while(ifstream1.getline(buffer2, sizeof(buffer2))){
cout<<buffer2<<endl;
}
ifstream1.clear(std::ios::goodbit);
ifstream1.seekg(std::ios::beg);
// 读文件的第四种方式
char c;
while((c=ifstream1.get())!=EOF){
cout<<c;
}
// 6.关闭文件
ifstream1.close();
}
int main() {
test01();
return 0;
}
二进制文件的写操作:
使用成员函数write进行写操作。
函数原型:ofstream& write(const char * buffer,int len);
参数:buffer是一个字符指针,指向内存中一段存储空间,len是读写的字节数。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void test01(){
ofstream ofstream1;
ofstream1.open("test.txt",ios::out|ios::binary);
string s="hello world";
ofstream1.write((const char*)&s, sizeof(s));
ofstream1.close();
}
int main() {
test01();
return 0;
}
二进制文件读操作:
使用成员函数read进行读操作。
函数原型:ifstream& read(char * buffer,int len);
参数:buffer是一个字符指针,指向内存中一段存储空间,len是读写的字节数。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void test01(){
ifstream ifstream1;
ifstream1.open("test.txt",ios::in|ios::binary);
if(!ifstream1.is_open()){
cout<<"文件打开失败"<<endl;
return;
}
string s;
ifstream1.read((char *)&s, 40);
cout<< s<<endl;
ifstream1.close();
}
int main() {
test01();
return 0;
}
潘芝 发布了15 篇原创文章 · 获赞 8 · 访问量 5930 私信 关注
内容总结
以上是互联网集市为您收集整理的C++学习笔记——文件操作全部内容,希望文章能够帮你解决C++学习笔记——文件操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。