文件与流(C++)

六、文件与流

6.1 IO流库概念

C++建立了一个十分庞大的流类库来实现数据的输入输 出操作。其中的每个流类实现不同的功能,这些类通过 继承组合在一起。

在这里插入图片描述

streambuf主要作为其他类的支持,定义了对缓冲区的 通用操作,如设置缓冲区、从缓冲区中读取数据,写入 数据等操作。

在这里插入图片描述

为了便于程序数据的输入输出,C++预定义了几个标准 输入输出流对象:

cout: ostream cout, 与标准输出设备关联

cin: istream cin, 与表示输入设备关联

cerr: ostream cerr, 与标准错误设备关联(非缓冲方 式)

clog: ostream clog, 与标准错误设备关联(缓冲方式)

6.2 istream 与 ostream

6.2.1 istream

#include <iostream>
using namespace std;

int main(void){

	char a[100] = {0};
	
	/* 
	 * 从标准输出设备输出
	 * */
	cout << "Please input a string: " ;

	/* 从标准输入设备接受 放到 a 当中去
	 * 遇到空格就结束
	 * */
	cin >> a;							 
	cout << a << endl;
	return 0;
}

//输出结果
myubuntu@ubuntu:~/lv19/cplusplus/dy08$ ./a.out 
Please input a string: 12 34
12

istream类定义了许多用于从流中提取数据和操作文件的成员函数,并对流的析取运算符 >> 进行了重载,实现了对内置数据量(基本数据类型)的输入功能。其中常用的几个成员函数时 get、getline、read、ignore.

get( char_type& ch); //从输入流中提取一个字符(包括空白符)
//一次性读取一行字符
getline( char_type* s, std::streamsize count, char_type delim );
//从输入流一次性读取count个字符
read( char_type* s, std::streamsize count );
//从输入流中读取字符并丢弃
ignore( std::streamsize count = 1, int_type delim = Traits::eof() );
#include <iostream>
using namespace std;

int main(void){

	char a[100] = {0};
	char c;
	
	/* 
	 * 从标准输出设备输出
	 * */
	cout << "Please input a string: " ;

	/* 从标准输入设备接受 放到 a 当中去
	 * 遇到空格就结束
	 * */
//	cin >> a;							 
	cin.getline(a, 100); //遇到空格就不会结束了 可以自定义结束符
	cout << a << endl;

	/*
	 * 从输入设备接收单个字符 存放到c字符变量当中 并循环输出
	 * */
	cout << "use get(); please input char: " << endl;
	while ((c = cin.get()) != '\n') 
		cout << c ;
	cout << endl;

	/*
	 * 从标准输入设备接受5个字符 并放到a数组当中
	 * */
	cout << "use get(a, 5) : " << endl;
	cin.get(a, 5);

	cout << a << endl;
	return 0;
}

6.2.2 ostream

ostream类提供了许多用于数据输出的成员函数,并通过流的输出<<重载,实现了对内置数据类型的输出功能。其中几个常用的成员函数时put、write、flush.

put( char_type ch ); //插入一个字符到输出流
write( const char_type* s, std::streamsize count );//插入一个字符序列到输出流中
flush(); //刷新输出流
#include <iostream>
#include <cstring>
using namespace std;

int main(void){

	char a[100] = "哈喽";
	cout << "word" << endl;
	cout.put('h').put('e').put('l').put('l').put('o').put('\n');

	cout.write(a, strlen(a));
	return 0;
}

6.2.3 输入输出的格式控制

C语言中可以使用scanf() 和 printf() 实现数据的格式化输入输出,C++中利用ios类的格式控制成员函数或者操纵符进行输入,输出数据的格式化。

  • 操纵符(全局函数)

    输入/输出操纵符 (gitee.io)

    在这里插入图片描述

    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main(void) {
    
    	cout << 10/3.0 << endl;
    	cout << setprecision(10) << 10/3.0 << endl; //输出精度为10
    
    	cout << setw(5) << 10 << endl; //右对齐 宽度为5个字符
    	cout << setw(5) << setfill('0') << 123 << endl; //空白地方填充字符 0
    	return 0;
    }
    //输出结果
    myubuntu@ubuntu:~/lv19/cplusplus/dy08$ ./a.out 
    3.33333
    3.333333333
       10
    00123
    
  • 成员函数

    std::basic_ostream (gitee.io)

    在这里插入图片描述

    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main(void) {
    
    	cout << 10/3.0 << endl;
    	//cout << setprecision(10) << 10/3.0 << endl; //输出精度为10
    	cout.precision(10); //输出精度为10
    	cout << 10/3.0 << endl;
    
    	cout << setw(8) << left << setfill('0') << hex << showbase << 123 <<endl;
    
    	return 0;
    }
    myubuntu@ubuntu:~/lv19/cplusplus/dy08$ ./a.out 
    3.33333
    3.333333333
        0x7b
    

6.3 string流

std::istrstream (gitee.io)

在这里插入图片描述

istrstream和ostrstream在98标准中废弃,取而代之的是istringstream和ostringstream,实现类似于C语言中sprint和sscanf的效果,不是在标准输入设备中获取数据,操作对象是在缓冲区。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
using namespace std;

int main(void) {

	int i = 10;
	double j = 3.14;
	char s[] = "hello";

/*
	char buf[100] = {0};
	sprintf(buf, "%d %lf %s", i, j, s);
	printf("%s\n", buf);

	char str[] = "12 4.15 world";
	sscanf(str, "%d %lf %s", &i, &j, s);
	printf("%d %lf %s\n", i, j, s);
*/

	ostringstream oss;
	oss << i << ' ' << j << ' ' << s ;
	cout << oss.str() << endl;

	istringstream iss;
	iss.str("46 6.54 leep");
	iss >> i >> j >> s ;
	cout << i << "," << j << ", " << s << endl;
	return 0;
}
myubuntu@ubuntu:~/lv19/cplusplus/dy08$ ./a.out 
10 3.14 hello
46,6.54, leep

6.4 文件流

C++将文件看成是一个个字符在磁盘上的有序集合,用流来实现文件的读写操作。C++中用来建立流对象的类有ifstream(输入)、ofstream(输出)、fstream(输入输出).

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

int main(void){

	int i = 12;
	double j = 3.14;
	char s[] = "hello";

	ofstream ofs("1.txt"); //如果没有1.txt 文件 会自动创建 并自动打开
	ofs << i << " " << j << " " << s << endl;
	ofs.close();

	ifstream ifs("1.txt");
	int i2;
	double j2 ;
	string s2;
	ifs >> i2 >> j2 >> s2;

	cout << i2 << endl;
	cout << j2 << endl;
	cout << s2 << endl;

	ifs.close();
	return 0;
}

//输出结果
myubuntu@ubuntu:~/lv19/cplusplus/dy08$ cat 1.txt 
12 3.14 hello
myubuntu@ubuntu:~/lv19/cplusplus/dy08$ ./a.out 
12
3.14
hello
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值