c++ primer plus 第十七章 输入 输出 文件 IO iostream fstream

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;


struct times
{
	int YEAR;
	int MON;
	int DAY;
}timeNow = 
{
	2016,
	3,
	5
};

int main()
{
	//1,ostream的其他方法
	//put
	//原型 ostream & put(char)
	cout.put('a');

	char str[10] = "bcdefg";

	cout.put(str[0]).put(' ');

	cout.put(*str+1).put('\n');
	cout << "=====================\n";
	//write
	//原型 basc_ostream<charT,traits>& write(const char_type* s, streamsize n);
	cout.write(str,3).put('\n');

	cout.write(str,10); //注意并不会停止 越界
	cout << endl;



	//cout 格式输出

	//进制
	//oct 8进制
	oct(cout);
	cout << 16 << endl;

	//dec 10进制
	dec(cout);
	cout << 16 << endl;

	//hex 16进制
	hex(cout);
	cout << 16 << endl;

	cout << "===================\n";
	cout << oct << 16 << " "<< dec << 16 << " " << hex << 16 << " " << endl;
	dec(cout); //恢复十进制
	//宽度
	cout << "===================\n";
	int orl = cout.width(12); //调节输出12的宽度 右对齐 返回以前设置的宽度
	int orl1 = cout.width(); //获得当前字符宽度

	cout.fill('*');  //填充字符 (与宽度不同 这里设置一次知道重设以后都不变)
	cout << "hello" << endl;
	cout << "orl: " << orl  << "orl1: "<< orl1 <<  endl;

	//设置小数点精度(c++ 默认6位)
	cout.precision(5);
	cout << 2.123456789 << endl;
	cout << 2.000000000 << endl; //0自动末尾舍弃
	cout.setf(ios_base::showpoint); //显示末尾0;
	cout << 2.0000000000 << endl;

	//setf
	//ios_base::boolalpha 输出输入布尔为 true 或者false
	cout.setf(ios_base::boolalpha);
	cout << true << endl;
	//ios_base::showbase  输出使用c++基数前缀(0,0x)
	//ios_base::showpoint  显示末尾的小数
	//ios_base::uppercase 十六进制使用大写 E表示法
	//ios_base::showpos 正数前面加上+

	//setf(long,long)
	//(1
	//第一个参数
	//ios_base::dec 使用基数10(十进制)
	//ios_base::oct 使用基数8(8进制)
	//ios_base::hex 使用基数16(16进制)
	//第二个参数
	//ios_base::basefield

	//(2
	//第一个参数
	//ios_base::fixed 使用定点计数法
	//ios_base::scientific 使用科学计数法
	//第二个参数
	//ios_base::floatfield

	//(3
	//第一个参数
	//ios_base::left 使用左对齐
	//ios_base::right 使用右对齐
	//ios_base::internal 符号或者基数前缀左对齐 值右对齐
	//第二个参数
	//ios_base::adjustfield;

	//iomanip 头文件

	//setprecision 设置精度 (注意要四舍五入)
	cout << setprecision(5) << 2.123456789 << endl;

	cout << setprecision(0); //恢复精度(默认6位)
	cout << 2.123456789 << endl;
	//setw 输出宽度 
	cout << setw(10) << 10 << endl;
	//setfill() 填充字符
	cout << setw(10) << setfill(' ')  << setfill('@') << 2 << endl; //清除前面的cout.fill  然后用setfill()设置填充符号




	//文件

	// ofstream 输出文件流
	ofstream outFile;
	outFile.open("file.txt");

	//ofstream outFile("files.txt"); 同上两步

	//ofstream继承ostream 所以可以使用它的所有方法 如下
	outFile << "hello";
	outFile.put('1');
	char strToFile[100] = "2222222";
	outFile.write(strToFile,3);
	outFile.close(); //关闭文件流

	//ifstream 输入文件流
	ifstream inFile("file.txt");
	if(!inFile.is_open())
	{
		cout << "抱歉文件打开失败!";
		return 0;
	}

	//ifstream 继承与istream 所以可以使用它的所有方法

	char strFile[100];
	inFile >> strFile;
	cout << strFile << endl;
	inFile.close();

	inFile.open("file.txt");
	if(!inFile.is_open())
	{
		cout << "抱歉文件打开失败!";
		return 0;
	}

	char ch;
	inFile.get(ch);
	cout << ch << endl;

	char strFile1[100];
	inFile.getline(strFile1,4);
	cout << strFile1 << endl;

	inFile.close();
	//检查文件流状态
	//is_open()
	//fail()
	//good()
	//eof

	//fstream 可以读写的文件

	//文件模式
	//ios_base::in //打开文件以便读取
	//ios_base::out //打开文件以便写入
	//ios_base::ate //打开文件并移到文件尾
	//ios_base::app //追加到文件尾
	//ios_base::trunc //如果文件存在则截断文件
	//ios_base::binary //二进制文件


	outFile.open("file.txt",ios_base::out|ios_base::app); //追加在文件末尾

	outFile << "!!!!"; //追加在文件末尾
	outFile.close();


	//二进制文件

	//write

	fstream biFile("binaryfile.txt",ios_base::binary|ios_base::out);
	biFile.write((char*)&timeNow,sizeof timeNow);

	biFile.close();

	//read
	times timeNowOne;
	biFile.open("binaryfile.txt",ios_base::binary|ios_base::in);
	biFile.read((char*)&timeNowOne,sizeof timeNowOne);
	cout << timeNowOne.YEAR << " " << timeNowOne.MON << " " << timeNowOne.DAY << endl;
	biFile.close();


	//随机存取

	//seekg() 将输入指针移到指定的位置
	//原型 basic_istream<charT, traists>& seekg(off_type, ios_base::seekdir);
	//seekdir:
	//ios_base::beg //开始
	//ios_base::cur //当前
	//ios_base::end //结尾

	//原型 basic_istream<charT, traits>& seekg(pos_type) 
	//pos_type // 相对于开始以一个为0 开始算(如 .seekg(12) 指向文件中的第13个字节)

	//seekp() 将输出指针移到指定的位置

	//tellg() //汇报输入指针的当前位置
	//tellp() //汇报输出指针的当前位置


	system("pause");
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值