IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++文件操作解析及使用(读、写文件 使用文件指针) -> 正文阅读

[C++知识库]C++文件操作解析及使用(读、写文件 使用文件指针)

进行文件I/O操作的时候要使用<fstream>头文件包含了很多标准库

下面是测试代码 创建文件并往文件中写入内容?

当文件夹中没有下面的文件时会创建,并且会覆盖原文件重新写入

一般是创建在编译器的那个 文件夹里

#include<iostream>
#include<strstream>
#include<fstream>
#include<iomanip>
using namespace std;
int main() {
	/*
	ofstream ofile;
	cout << "create file1" << endl;
	ofile.open("test.txt");
	if (!ofile.fail())
	{
		ofile << "name1" << " ";
		ofile << "sex1" << " ";
		ofile << "age1" << " ";
		ofile.close();
		cout << "create file2" << endl;
		ofile.open("test2.txt");
		if (!ofile.fail()) {

			ofile << "name2" << " ";
			ofile << "sex2" << "";
			ofile << "age2" << "";
			ofile.close();


		}

2:下面的测试代码用了ifstream和ofstream对象实现读写文件的功能

用户手动输入5次数据 程序把它写入文件中 然后再打开

#include<iostream>
#include<strstream>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
char buf[128];
	ofstream ofile("test.txt");
	for (int i = 0; i < 5; i++) {
		memset(buf, 0, 128);//输入五次数据 程序把这五次数据写入文件中
		cin >> buf;
		ofile << buf;


	}

	ofile.close();//关闭
	ifstream ifile("test.txt");//然后再用ifstream打开
	while (!ifile.eof()) {

		char ch;
		ifile.get(ch);
		if (!ifile.eof())
			cout << ch;
	}
	cout << endl;
	ifile.close();
}

3:对二进制文件的读写 要用 ios::binary模式 读取需要read方法 写则需要write方法

#include<iostream>
#include<strstream>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
char buf[50];
	fstream file;
	file.open("test.dat", ios::binary | ios::out);
	for (int i = 0; i < 2; i++) {
		memset(buf, 0, 50);
		cin >> buf;
		file.write(buf, 50);//二进制写要用 write方法
		file << endl;
	}
	file.close();
	file.open("test.dat", ios::binary | ios::in);
	while (!file.eof())
	{
		memset(buf, 0, 50);
		file.read(buf, 50);//二进制读取要read方法
		if (file.tellg() > 0)
			cout << buf;


	}
	cout << endl;
	file.close();
	return 0;
}

4:文件的追加

在写入文件时 你可以不一次性写入全部数据 而是一部分一部分写 即不会覆盖前面的内容

测试代码如下

#include<iostream>
#include<fstream>
using namespace  std;
int main() {
	/*
	ofstream ofile("test.txt", ios::app);
	if (!ofile.fail()) {
		cout << "start write" << endl;
		ofile << "mary  ";
		ofile << "girl  ";
		ofile << "24  ";




	}
	else
		cout << "打不开啊";
}

5:eof成员函数可以用来判断文件结尾?

在指定位置读写文件? 用到了文件指针 可以输出你要求的位置的信息

?测试代码如下

#include<iostream>
#include<fstream>
using namespace  std;
int main() {
ifstream ifile;
	char cfileselect[20];
	cout << "输入文件名:";
	cin >> cfileselect;
	ifile.open(cfileselect);
	if (!ifile)
	{
		cout << cfileselect << "打不开" << endl;
		return 0;

	}
	ifile.seekg(0, ios::end);
	int maxpos = ifile.tellg();
	int pos;
	cout << "输入查找的位置:";
	cin >> pos;
	if (pos > maxpos) {
		cout << "is over file lenght" << endl;
	}
	else
	{
		char ch;
		ifile.seekg(pos);
		ifile.get(ch);
		cout << ch << endl;
	}
	ifile.close();
	return 1;







}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-04 00:51:43  更:2022-09-04 00:54:49 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 8:38:08-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码