`
mywebcode
  • 浏览: 999074 次
文章分类
社区版块
存档分类
最新评论

C++ I/O

 
阅读更多

1 cin,>>

作用:从标准输入设备中读取数据,忽略空格、制表符、回车
>> :析取运算符

2 cin和 get,ignore,putback,peek

get(ch):将输入流中所有字符,包括空白符,读入到参数列表中指定变量对应的内存空间中
cin.ignore(intExp,chExp):忽略掉接下来的intExp个字符,或者首次遇到chExp以前的所有字符
putback:将get函数在输入流中读取的最后一个字符回退到输入流中
peek:检查输入流,并且告诉程序输入流中下一个字符是什么,但并不实际读取这个字符。

示例代码:
#include <iostream>
using namespace std;

int main() {
	char ch;
	cout << "Enter a String :" << endl;
	// 输入:abcd

	cin.get(ch);// 取出输入流中第一字符 ch = a,  输入流中剩余:bcd
	cout << "ch = " << ch << endl;
	cin.get(ch);// 取出输入流中第一字符 ch = b,  输入流中剩余:cd
	cout << "ch = " << ch << endl;
	cin.putback(ch); // 将ch =b , 回退到输入流中,输入流中剩余:bcd
	cin.get(ch);// 取出输入流中第一字符 ch = b,  输入流中剩余:cd
	cout << "ch = " << ch << endl;
	ch = cin.peek(); // 尝试读取输入流中的第一字符,单并没有取走,输入流中剩余:cd
	cout << "ch = " << ch << endl;
	cin.get(ch);// 取出输入流中第一字符 ch = c,  输入流中剩余:d
	cout << "ch = " << ch << endl;

	return 0;
}
输出结果:
Enter a String :
abcd
ch = a
ch = b
ch = b
ch = c
ch = c

3 clear

当输入流遇到错误是,系统将忽略掉所有使用该输入流的I/O语句。可以使用Clear函数将输入流恢复到正常状态。
示例代码:
#include <iostream>
using namespace std;

int main() {
	int a = 23;
	int b = 34;
	cout << "Enter a number followed by a character:" << endl;
	cin >> a >> b;
	cout << "a="<< a << ",b=" << b << endl;
	cin.clear();// 将输入流变为正常状态,没有此句会直接结束
	cin.ignore(200,'\n');
	cout << "Enter two numbers:" << endl;
	cin >> a >> b;
	cout << "a="<< a << ",b=" << b << endl;
	return 0;
}
运行结果:
Enter a number followed by a character:
232a
a=232,b=34
Enter two numbers:
23 21
a=23,b=21

4 setprecision,fixed,showpoint,setw

头文件:#include<iomanip>
setprecision:控制输出浮点数小数精度 ,
fixed:可以使输出的浮点数按照固定小数点个数输出,取消方法 cout.unsetf(ios::fixed)
showpoint:当计算机输出以固定小数点个数输出小数是,如果小数点后部分的值是0,那么默认输出的数字将没有小数点和小数点后面的数字,showpoint控制符可以强制显示输出小数点和小数点后面的0

示例代码:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	double x, y, z;
	x = 15.572;
	y = 1234.43;
	z = 9823.2385;


	cout << fixed << showpoint;
	cout << setprecision(2) << "setprecision(2)" << endl;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	cout << "z = " << z << endl;
	cout << setprecision(3) << "setprecision(3)" << endl;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	cout << "z = " << z << endl;
	cout << setprecision(4) << "setprecision(4)" << endl;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	cout << "z = " << z << endl;

	cout << setprecision(3) << x << " " << setprecision(2) << y << " " << setprecision(4) << z << endl;


	return 0;
}

运行结果:
setprecision(2)
x = 15.57
y = 1234.43
z = 9823.24
setprecision(3)
x = 15.572
y = 1234.430
z = 9823.238
setprecision(4)
x = 15.5720
y = 1234.4300
z = 9823.2385
15.572 1234.43 9823.2385

可以用setf来指定fixed,scientific,showpoint。格式标志:ios::fixed,ios::scientific,ios::showpoint,。ios::fixed,ios::scientific 都是C++数据类型ios::floatfield的一部分。当指定fixed或者scientific标志时,必须保证
ios::fixed 和ios::scientific 不能同时出现,而且必须保证ios::floatfield作为函数setf的第二个参数使用。

cout.setf(ios::fixed,ios::floatfield)
cout.setf(ios::showpoint)

另外还可以:

cout << setiosflags(ios::fixed);
cout << setiosflags(ios::showpoint)
或:cout << setiosflags(ios::fixed | ios::showpoint)

setw:指定输出表达式值占用的列数,默认是右对齐。
示例代码:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int x = 19;
	int a = 345;
	double y = 76.384;

	cout << fixed << showpoint;

	cout << "12345678901234567890" << endl;
	cout << setw(5) << x << endl;
	cout << setw(5) << a << setw(5) << "Hi" << setw(5) << x << endl << endl;

	cout << setprecision(2);
	cout << setw(6) << a << setw(6) << y << setw(6) << x << endl;
	cout << setw(6) << x << setw(6) << a << setw(6) << y << endl;

	cout << setw(5) << a << x << endl;
	cout << setw(2) << a << setw(4) << x << endl;


	return 0;
}
运行结果:
12345678901234567890
19
345 Hi 19


345 76.38 19
19 345 76.38
34519
345 19

5 fill,setfill,left,right

fill,setfill 设置没有数据列,填充除空格符以为的其他字符
cout.fill('*');
cout << setfill('*');

示例代码:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int x = 19;
	int y = 7624;

	cout << "12345678901234567890" << endl;
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;
	cout.fill('*');
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;

	cout << setw(5) << x << setw(7) << setfill('#') << y << setw(8) << "warm" << endl;
	cout << setw(5) << setfill('@') << x << setw(7) << setfill('#') << y << setw(8) << setfill('^')<< "warm" << endl;
	cout.fill(' ');
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;

	
	return 0;
}
运行结果:
12345678901234567890
19 7624 warm
***19***7624****warm
***19###7624####warm
@@@19###7624^^^^warm
19 7624 warm

left,right 控制左对齐或者右对齐
cout << left
cout.unsetf(ios::left)

示例代码:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int x = 19;
	int y = 7624;

	cout << left;
	cout << "12345678901234567890" << endl;
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;
	cout.fill('*');
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;

	cout << setw(5) << x << setw(7) << setfill('#') << y << setw(8) << "warm" << endl;
	cout << setw(5) << setfill('@') << x << setw(7) << setfill('#') << y << setw(8) << setfill('^')<< "warm" << endl;
	
	cout.unsetf(ios::left);
	cout.fill(' ');
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;

	
	return 0;
}
运行结果:
12345678901234567890
19 7624 warm
19***7624***warm****
19***7624###warm####
19@@@7624###warm^^^^
19 7624 warm

在标准C++ 中,left和right控制符只可以作为格式标志使用。因此,必须以setf和 setiosflags控制符来设置left和right

6 flush

清空缓冲区

7 string I/O

从标准设备中获取一行字符串:getline(cin,str)

8 file I/O

文件I/O 操作的5个步骤
1,#include<fstream>
2,定义文件流变量
3,将文件流与输入输出文件关联
4,读写文件
5,关闭文件

fileStreamVariable.open(sourceName,fileOpeningMode)

打开方式:
ios::in 以输入方式打开,ifstream类型默认打开方式
ios::out 以输出方式打开,ofstream类型默认打开方式
ios::nocreate 如果文件不存在,则打开操作失败
ios::app 如果文件存在,则从文件的末尾开始写入数据,如果不存在创建一个新文件
ios::ate 以输出方式打开文件将文件读写位置移到文件末尾。输出数据可以写到文件的任何地方
ios::trunc 如果文件存在,其中的内容将被删除(截掉)
ios::noreplace 如果文件存在,则打开操作失败

#include<fstream>
#include<stream>
using namespace std;
int main()
{
	fstream fin("data.txt");  //打开文件
	string ReadLine;
	while(getline(fin,ReadLine))  //逐行读取,直到结束
	{
		...
	} 
	fin.close();
	return 0
}

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
	int test1,test2,test3,test4,test5;
	char id;
	double avg;

	ifstream ifs("test.txt");

	if (!ifs)
	{
		cout << "Cannot open input file." << endl;
		return 1;
	}

	ofstream ofs("result.txt");
	ifs >> id >> test1 >> test2 >> test3 >> test4 >> test5;

	avg = (test1 + test2 + test3 + test4 + test5) / 5.0;
	ofs << fixed << showpoint << setprecision(2);
	ofs << setw(25) << right << "Student Id:" << setw(8) << id << endl;
	ofs << setw(25) << right << "Test Score:" << setw(8) << test1 << setw(8) << test2 << setw(8) << test3 << setw(8) << test4 << setw(8) << test5 << endl;
	ofs << setw(25) << right << "Average test score:" << setw(8) << avg << endl;

	ifs.close();
	ofs.close();
}


9 总结:

  1. C++ 中的流是从源到目标的一系列字符的有限序列
  2. 输入流是从源到计算机的流
  3. 输出流是从计算机到目标的流
  4. cin代表标准输入,是输入流对象,通常初始化为标准输入设备——键盘
  5. cout代表标准输出,是输出流对象,通常初始化为标准输出设备——屏幕
  6. 双目运算符>>和输入流对象,如cin,结合使用时被称为流析取运算符。 >> 左边的对象必须是输入流对象,如cin,右边的操作数必须是变量
  7. 双目运算符<<和输出流对象,如cout,结合使用时被称为流插入运算符。 << 左边的对象必须是输出流对象,如cout,右边的操作数必须是变量
  8. 当使用运算符>> 将数据读入变时,将忽略掉数据前的所有空白符
  9. 为了使用cin和cout,程序必须包含iostream文件
  10. get 函数用于逐个读入字符,不会忽略到任何空白符
  11. ignore函数用于在一行中略掉某些数据
  12. putback函数用于将get函数读入的最后一个字符退回到输入流中
  13. peek函数返回输入流中的当前字符,但是并不将该字符重输入流中移走
  14. 试图将非法数据读入变量将导致输入流进入错误状态
  15. 一旦输入流进入错误状态,可以使用clear函数将输入流恢复成正常状态
  16. setprecision控制符用来设置输出浮点数的精度
  17. fixed控制符将浮点数以固定小数点个数输出
  18. showpoint控制符用来指定浮点数必须包含小数点和小数点末尾的零
  19. setw控制数据列宽,默认右对齐
  20. 如果在setw 中指定的输出列宽小于实际数据需要,数据不会被截短,而是按照实际宽度输出
  21. get,ignore,put,peek,fill,clear,setf,unsetf流函数头文件在 iostream中
  22. setprecision,setw,setfill,setiosflags在iomanip中
  23. 头文件fstream包含ifstream和ofstream定义
  24. 文件打开后需要关闭
分享到:
评论

相关推荐

    C++ I/O流课件 很不错的哦

    C++ I/O流课件C++ I/O流课件 很不错的哦

    C++I/O描述

    介绍了 C++的 I/O( 输入输出 )问题,本文结构清晰,讲解简单易懂,对 C++ 的流模型作为细致的分析和讲解, 并介绍了 IO 流类中的各种成员函数,对于输出部分介绍了 cout 流, put 和 write 函数,介绍了 4 种控制...

    c++ I/O流的常用控制符 C++常用特殊字符

    c++ I/O流的常用控制符 C++常用特殊字符 便于查找 很方便的小文档

    C++ I/O Streams ppt

    Chapter 2 I/O Streams as an introduction to Objects and Classes

    实验七 C++的I/O流(验证性)

    实验七 C++的I/O流(验证性)实验报告 doc 格式

    c / c++ / cpp / stl 中文帮助文档手册chm格式下载

    C++ I/O C++ Strings C++ 标准模板库 C++ Bitsets C++ Double-Ended Queues C++ Lists C++ Maps C++ Multimaps C++ Multisets C++ Priority Queues C++ Queues C++ Sets C++ Stacks C++ ...

    C++ I/O文件读写操作的示例代码

    主要介绍了C++ I/O文件读写操作的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    用C++实现简单的文件I/O操作

    本文给大家分享了用C++实现简单的文件I/O操作。

    C++的I/0操作

    (1)掌握在自定义的类中重载提取运算符&lt;&lt;和插入运算符&gt;&gt; 并用它们输入 输出本类对象 2 掌握文件操作的步骤和方法 能利用程序建立数据文件 打开数据文件并进行相关操作

    C和C++参考手册chm版

    C++ I/O C++ Strings C++ String Streams Miscellaneous C++ C++ Standard Template Library About the Standard Template Library Iterators C++ Algorithms C++ Vectors C++ Double-Ended Queues C++ ...

    C/C++语言参考(英文版)

    C++ I/O C++ Strings C++ String Streams Miscellaneous C++ C++ Standard Template Library About the Standard Template Library Iterators C++ Algorithms C++ Vectors C++ Double-Ended Queues ...

    All About: File I/O in C++

    &lt;br&gt;Introduction &lt;br&gt;This tutorial will start with the very basis of File I/O (Input/Output) in C++. After that, I will look into aspects that are more advanced, showing you some tricks, and ...

    非常好用的C和C++函数库电子手册

    C++ I/O C++ Strings C++ String Streams Miscellaneous C++ C++ Standard Template Library C++ Algorithms C++ Vectors C++ Double-Ended Queues C++ Lists C++ Priority Queues C++ Queues C++ Stacks ...

    C/C++函数手册-中文版和英文版

    C++ I/O C++ Strings C++ 标准模板库 C++ Bitsets C++ Double-Ended Queues C++ Lists C++ Maps C++ Multimaps C++ Multisets C++ Priority Queues C++ Queues C++ Sets C++ Stacks C++ Vectors ...

    C /C++库函数及文件大全 经典 chm

    Character Standard C Math Standard C Time & Date Standard C Memory Other standard C functions All C Functions C++ C++ I/O C++ Strings C++ String Streams ...

    C_C++语言参考.

    C++ I/O C++ Strings C++ 标准模板库 C++ Bitsets C++ Double-Ended Queues C++ Lists C++ Maps C++ Multimaps C++ Multisets C++ Priority Queues C++ Queues C++ Sets C++ Stacks C++ Vectors ...

    C++完全帮助文档(手册)

    C++ I/O o C++ String Streams * C++ Exceptions * Complex Numbers C++ Standard Template Library (STL) * Overview * C++ Algorithms * C++ Vectors * C++ Double-Ended Queues * C++ Lists ...

    深入详解C++ IO

    全面而深入的c++ I/O用法详解。《深入详解C++ IO》它将让你精通I/O操作,体会c++ I/O的强大功能。

    C++语言精华 详细

    除了完全支持C的I/O系统外C++还定义了自己的面向对象的I/O系统。和C的I/ O系统一样,C++的I/O系统也完全是集成化的,即c++的I/O系统的那些有差别的地 方,如控制台I/O和磁盘I/O,实际上只是相同机制的不同...

    采用C++语言实现的WSAEventSelect I/O模型

    采用C++语言编写,在VS2010下开发,可以直接运行,代码中有相关的注释。

Global site tag (gtag.js) - Google Analytics