C++:文件及输入输出流

I/O 流,流类库

输入流:与输入设备(如键盘)相联系的流

输出流:与输出设备(如显示器)相联系的流

输入/输出流: 与输入输出设备(如磁盘)相联系的流

流类:C++中预先定义了一组类,用于处理输入/输出

流类库:C++中所有的流类组成的集合

streambuf 类(底层)

派生类

1. filebuf 类:使用文件来保存缓冲区的字符序列

2. strstreambuf 类:在内存中进行提取和插入操作的缓冲区管理

3. conbuf 类:提供控制光标、设置颜色、定义活动窗口、清屏、清行等功能

ios 类 (高层)

直接派生类

1. 输入流:istream

2. 输出流:ostream

3. 文件流:fstreambase

4. 串流:strstreambase

键盘输入与屏幕输出

一般的输入/输出

数据传输方法:

1. 使用在标准输入/输出头文件 stdio.h 中声明的库函数

2. 使用标准命名空间 std 中的输入输出流类文件 iostream 中定义的流对象 cin 和 cout

3. 使用 << 和 >> 

#include <iostream>
#include <string>

using namespace std;

const int size = 80;

int main()
{
    char buffer[size],buffer2[size];
    char buffer3[size];
    string str1,str2;
    cout << "Enter a sentence: ";
    cin >> buffer;
    cout << "buffer with cin: " << buffer << endl;

    cin.get(buffer2,size);
    cout << "buffer2 with cin.get: " << buffer2 << endl;

    cout << "Enter a sentence: ";
    cin.getline(buffer3,size);
    cout << "buffer3 with cin.getline: " << buffer3 << endl;
    
    cout << "Enter strings: ";
    cin >> str1 ;
    cout << "str1 with cin: " << str1 << endl;

    getline(cin,str2);
    cout << "str2 with getline: " << str2 << endl;
    
    return 0;
}

格式化输入/输出

#include <iostream>
using namespace std;

int main() 
{
    cout.setf(ios::showpos);    //符号显示
    cout.setf(ios::scientific); //科学计数法
    cout << 123 << " " << 123.3 << endl;
    
    cout.unsetf(ios::showpos);   //符号不显示
    cout << 123 << " " << 123.3 << endl;

    return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
    int i=6879;
    int j=1234;
    int k=-10;
    cout<<setw(6)<<i<<j<<k<<endl;
    cout<<setw(6)<<i<<setw(6)<<j<<setw(6)<<k<<endl;
    return 0;
}

自定义控制符函数

#include <iostream>
#include <iomanip>
using namespace std;

ostream &setup(ostream &stream)
{
    stream.setf(ios::left);
    stream << setw(10)<<setfill('$');
    return stream;
}

int main() 
{
    cout << 10 << "Hello" << endl;
    cout << setup << 10 << "Hello" << endl;
    cout << setup << 10 << 10 << "Hello" << endl;
   return 0;
}

重载提取运算符和插入运算符 

#include <iostream>
using namespace std;

class CDate
{
private:
    int Date_Year;
    int Date_Month;
    int Date_Day;
public:
    friend istream & operator >> (istream &input, CDate &Date);
    friend ostream & operator << (ostream &output, const CDate &Date);
};

istream & operator >> (istream &input, CDate &Date)
{
    input >> Date.Date_Year >> Date.Date_Month >> Date.Date_Day;
    return input;
}

ostream & operator << (ostream &output, const CDate &Date)
{
    output << Date.Date_Year << "-" << Date.Date_Month << "-" << Date.Date_Day;
    return output;
}

int main() 
{
   CDate d;
   cin >> d;
   cout << d << endl;
   return 0;
}

文件的输入/输出

cpp 读写文件操作:

1. 包含头文件 fstream

2. 定义一个文件流对象

3. 建立(或打开文件)

4. 进行读/写操作

5. 关闭文件

文件的打开与关闭

两种方式:

1. 先定义文件流对象,然后调用成员函数 open 打开文件

2. 在定义文件流对象时,直接利用其构造函数打开文件

get() 和 put() 一般成对使用,既可用于读写文本文件,也可用于读写二进制文件,每次读写一字节

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

void CreateFile(string filename);
void ReadFile(string filename);

int main() {
	CreateFile("test.txt");
	ReadFile("test.txt");
    return 0;
}

void CreateFile(string filename) {
	ofstream file(filename);
	if (file.is_open()) {
		file << "Hello, world!" << endl;
		file.close();
	}
	else {
		cout << "Error: Could not open file." << endl;
	}
}

void ReadFile(string filename) {
	ifstream file(filename);
	if (file.is_open()) {
		string line;
		while (getline(file, line)) {
			cout << line << endl;
		}
		file.close();
	}
	else {
		cout << "Error: Could not open file." << endl;
	}
}

read()  和 write() 一般成对使用,既可用于读写文本文件,也可用于读写二进制文件,每次读写一个数据块

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tsglz3210

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值