您的位置:首页 > 编程语言 > C语言/C++

C++ I/O

2015-07-17 15:41 302 查看

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 总结:

C++ 中的流是从源到目标的一系列字符的有限序列
输入流是从源到计算机的流
输出流是从计算机到目标的流
cin代表标准输入。是输入流对象,通常初始化为标准输入设备——键盘
cout代表标准输出,是输出流对象,通常初始化为标准输出设备——屏幕
双目运算符>>和输入流对象,如cin,结合使用时被称为流析取运算符。 >> 左边的对象必须是输入流对象。如cin,右边的操作数必须是变量
双目运算符<<和输出流对象。如cout,结合使用时被称为流插入运算符。 << 左边的对象必须是输出流对象。如cout。右边的操作数必须是变量
当使用运算符>> 将数据读入变时。将忽略掉数据前的全部空白符
为了使用cin和cout,程序必须包括iostream文件
get 函数用于逐个读入字符,不会忽略到不论什么空白符
ignore函数用于在一行中略掉某些数据
putback函数用于将get函数读入的最后一个字符退回到输入流中
peek函数返回输入流中的当前字符。可是并不将该字符重输入流中移走
试图将非法数据读入变量将导致输入流进入错误状态
一旦输入流进入错误状态,能够使用clear函数将输入流恢复成正常状态
setprecision控制符用来设置输出浮点数的精度
fixed控制符将浮点数以固定小数点个数输出
showpoint控制符用来指定浮点数必须包括小数点和小数点末尾的零
setw控制数据列宽,默认右对齐
假设在setw 中指定的输出列宽小于实际数据须要,数据不会被截短,而是依照实际宽度输出
get,ignore,put,peek,fill,clear,setf,unsetf流函数头文件在 iostream中
setprecision,setw,setfill,setiosflags在iomanip中
头文件fstream包括ifstream和ofstream定义
打开需要关闭该文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: