• c++ 文件操作 fstream


    5 文件操作

    程序运行时产生的数据都属于临时数据,程序一旦运行结束都会释放

    通过文件可以将数据持久化

    C++中对文件操作需要含文件头<fstream>

    文件类型分为两种:

    1. 文本文件 - 文件以文本的ASCII码形式存储在计算机中
    2. 二进制文件 - 文件以文本的 二进制 形式存储在计算机中,用户一般不能直接读懂他们

    操作文件的三大类:

    1. ofstream:写操作
    2. ifstream: 读操作
    3. fstream: 读写操作

    5.1 文本文件

    5.1.1 写文件

    写文件步骤如下

    • 1 包含头文件 #include <ofstream>
    • 2 创建流对象 fstream ofs;
    • 3 打开文件 ofs.open("文件路径",打开方式)
    • 4 写数据 ofs << "写入数据"
    • 5 关闭文件 ofs.close();

    文件打开方式

    打开方式 解释
    ios::in 为读文件而打开文件
    ios::out 为写文件而打开文件
    ios:: ate 初始位置:文件尾部
    ios::app 追加方式写入文件
    ios::trunc 如果文件存在,先删除,再创建
    ios::binary 二进制方式

    注意: 文件打开方式可以配合使用,利用|操作符

    例如:用二进制方式写文件 ios::binary|ios::in

    #include <iostream>
    #include <fstream>
    using namespace std;
    
    // 文本文件写文件
    
    
    void test01() {
    	// 1.包含头文件
    	// 2. 创建文件流对象
    	fstream ofs;
    	// 3. 打开文件
    	ofs.open("C:\\Users\\DSF-LSJ\\Desktop\\tests.txt",ios::out); // 没有文件就会创建文件
    	// 4. 写入文本
    	ofs << "hello word";
    	// 5. 关闭文件
    	ofs.close();
    }
    
    
    int main() {
    	test01();
    	return 0;
    }
    

    5.1.2 读文件

    读文件步骤如下

    • 1 包含头文件 #include <ofstream>
    • 2 创建流对象 fstream ifs;
    • 3 打开文件 ifs.open("文件路径",打开方式) 判断文件是否打开成功
    • 4 读数据 四种方式读取
    • 5 关闭文件 ofs.close();

    判读文件是否打开

    	if (!ifs.is_open()) {
    		cout << "文件打开失败" << endl;
    	}
    

    示例:

    //#include <iostream>
    //#include <fstream>
    //using namespace std;
    //
    //// 文本文件写文件
    //
    //
    //void test01() {
    //	// 1.包含头文件
    //	// 2. 创建文件流对象
    //	fstream ofs;
    //	// 3. 打开文件
    //	ofs.open("C:\\Users\\DSF-LSJ\\Desktop\\tests.txt",ios::out); // 没有文件就会创建文件
    //	// 4. 写入文本
    //	ofs << "hello word";
    //	// 5. 关闭文件
    //	ofs.close();
    //}
    //
    //
    //int main() {
    //	test01();
    //	return 0;
    //}
    
    // 读文件
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    void test01() {
    	// 1. 导入文件头
    	// 2. 创建文件流对象
    	fstream ifs;
    	// 3. 打开文件
    	ifs.open("C:\\Users\\DSF-LSJ\\Desktop\\test.txt", ios::in);
    
    	if (!ifs.is_open()) {
    		cout << "文件打开失败" << endl;
    	}
    	// 第一种 创建char数组 通过循环拿到
    	//char buf[1024] = { 0 }; 
    	//while (ifs>>buf) {
    	//	cout << buf << endl;
    	//}
    
    	// 第二种
    	char buf[1024] = { 0 };
    	while (ifs.getline(buf,sizeof(buf))) {
    		cout << buf << endl;
    	}
    	ifs.close();
    
    	// 第三种
    	// pass 全局getline ...
    
    	// 第四种
    	//char c;
    	//while ( (c = ifs.get()) != EOF) {  // EOF end of file 是否读到文件尾部
    	//	cout << c;
    	//}
    }
    
    int main() {
    	test01();
    	return 0;
    }
    

    5.2 二进制文件

    以二进制的方式进行读写操作

    打开方式指定为: ios::binary

    5.2.1 写文件

    二进制方式写文件主要利用对象调用成员函数write

    函数原型:ostream & write(const char *buffer ,int len)

    参数解释: 字符指针buffer 指向内存中一段存储空间,len是读写的字节数

    示例:

    #include <iostream>
    #include <fstream>
    using namespace std;
    // 二进制写文件
    class Person {
    public:
    	char m_Name[64];  // 姓名
    	int m_Age;        // 年龄
    };
    
    void test01() {
    	// 1.包含头文件
    	// 2.创建流对象
    	fstream ofs;
    	// 3.打开文件
    	ofs.open("test.txt",ios::out|ios::binary);
    	// 4.写入文件 *******************************
    	Person p = { "张三",18 };
    	ofs.write((const char*)&p, sizeof(Person));
    	// 5.关闭文件
    	ofs.close();
    }
    
    int main() {
    	test01();
    	return 0;
    }
    

    5.2.2 读文件

    二进制方式读文件主要利用流对象调用成员函数read

    函数原型:istream& read(char * buffer,int len)

    参数解释: 字符指针buffer指向一段内存空间,len是读写的字节数

    示例:

    
    #include <iostream>
    #include <fstream>
    using namespace std;
    // 二进制读文件
    class Person {
    public:
    	char m_Name[64];  // 姓名
    	int m_Age;        // 年龄
    };
    
    void test01() {
    	// 1.包含头文件
    	// 2.创建流对象
    	fstream ofs;
    	// 3.打开文件
    	ofs.open("test.txt", ios::in | ios::binary);
    
    	// 4.读文件 **************************
    	Person p;
    	ofs.read((char * )&p, sizeof(Person));
    	cout << p.m_Age << endl;
    	cout << p.m_Name << endl;
    
    	// 5.关闭文件
    	ofs.close();
    }
    
    int main() {
    	test01();
    	return 0;
    }
    
  • 相关阅读:
    #Git 21天打卡第一天 01天0526
    老徐第六期百人计划之职业发展方向&学习方向
    LR12.53安装中文补丁包,录制后回放脚本一致卡在编译的问题
    常用oracle语句整理
    LoadRunner11之批量插入SQL数据~2
    LoadRunner12之SQLServer数据库批量插入--.Net协议
    Jmeter连接Oracle数据库简单使用
    AppScan安装使用
    SQL多表连接
    [剑指Offer] 4.二维数组的查找
  • 原文地址:https://www.cnblogs.com/lddragon/p/16099499.html
Copyright © 2020-2023  润新知