C++_输入输出流

缓冲区的概念

char ch;
while ((ch = cin.get()) != EOF)
{
	cout << ch << endl;
}

输入:aabbccdd,系统会把输入的内容放在缓冲区中,然后get函数,从缓冲区中一个一个的读取。
只要没有碰到EOF(ctrl+z),程序就会一直执行。
在这里插入图片描述

cin.get(char a)用法

char a, b, c;
cin.get(a);
cin.get(b);
cin.get(c);
cout << a << b << c << endl;
cin.get(a).get(b).get(c);

输入adfdf,输出的结果是前三个,adf,因为只从缓冲区取前三个,后边的就不取了。

cin 和 getline()函数比较

char buf1[256];
char buf2[256];
cout << "请输入一个字符串,带空格" << endl;
cin >> buf1;
cin.getline(buf2, 256);
cout << "buf:" << buf1 << "buf2:" << buf2 << endl;
cout << "over" << endl;
system("pause");
return 1;

输出结果:
请输入一个字符串,带空格
afdfd fdsaf fdfd
buf:afdfdbuf2: fdsaf fdfd
over
**发现:**cin只取走缓冲区中空格前的内容,而getline则算上空格一块取走。

getline和ignore的使用

代码:

cout << "aaa ffff" << endl;
char buf1[256]; char buf2[256];
cin >> buf1;
cin.ignore(2);
cin.getline(buf2, 256);
cout << "buf:" << buf1 << "buf2:" << buf2 << endl;
cout << "over" << endl;

输出结果:aaa ffff
fff ddddd
buf:fffbuf2:ddddd
over
请按任意键继续. . .
ignore函数中的2,是去掉输入一行字符串的两个空格。

下面是标准输出流

/*
标准输出流对象cout
cout.flush()
cout.put()
cout.write()
cout.width()
cout.fill()
cout.setf(标记)
*/

cout.put()

代码如下:

cout << "hello" << endl;
cout.put('h').put('e').put('i');

输出结果:
hello
hei请按任意键继续. . .

cout.fill()和cout.width()

代码如下:

cout << "123456789" << endl;
cout.width(8);
cout << cout.fill('*') << 123 << endl;

输出结果:
123456789
******* 123
请按任意键继续. . .

**cout.write()
代码:

cout.write("333333", 3) << endl;

输出结果:
333
这个函数第一个参数是,输出的字符串第二个参数是要输出的字符串长度。

文件的输入和输出

代码:

char fname[] = "C:/2.txt";
	//char *p = fname;
	//ofstream fout(p,ios::binary);  //写文件,
	//fout << "hello.....1111" << endl;
	//fout << "hello.....2222" << endl;
	//fout << "hello.....3333" << endl;
	//fout << 22222222 << endl;
	//fout.close();
	读文件
	//ifstream fin(fname);  //读文件。
	//char ch;
	//while ((ch=fin.get())!=EOF)//一个一个字符读取。
	//{
	//	cout << ch;
	//}

**1 编程实现以下数据输入/输出:

**(1)以左对齐方式输出整数,域宽为12。**

long a = 234;
double b = 2345.67890;
char c[100];
cout.fill('*');   //左对齐的方式
cout.flags(ios_base::left);
cout.width(12);
cout << a << endl;

输出结果:
234*********

cout.fill('*');
cout.flags(ios::right);
cout.width(12);
cout << a << endl;

输出结果:
*********234

(2)以八进制、十进制、十六进制输入/输出整数。

cout.flags(ios::hex);// 设置缓冲区的方式。
cout << 234 << '\t';
cout.flags(ios::dec);
cout << 234 << '\t';
cout.flags(ios::oct);
cout << 234 << endl;

输出结果
这个十六进制ea 这是十进制234 这是八进制352

(3)实现浮点数的指数格式和定点格式的输入/输出,并指定精度。

cout.flags(ios::scientific);
cout << b << '\t';
cout.flags(ios::fixed);
cout << b << endl;

输出结果:
2.345679e+03 2345.678900

(4)把字符串读入字符型数组变量中,从键盘输入,要求输入串的空格也全部读入,以回车符结束。

cin.get(c, 99);
cout << c << endl;

输出:
43 radf fdfd
43 radf fdfd

(5)将以上要求用流成员函数和操作符各做一遍。**

2编写一程序,将两个文件合并成一个文件。

这个程序是把2.txt写到23.txt中。

char fname1[] = "C:\\2.txt";
char fname2[] = "C:\\23.txt";
ifstream fin1(fname1);
ifstream fin2(fname2);
char ch;
ofstream fout(fname2,ios::app);  //写文件
while ((ch=fin1.get())!=EOF)
{
	fout << ch;
	cout << ch;
}

3编写一程序,统计一篇英文文章中单词的个数与行数。

统计单词总数://读取方式: 逐词读取, 词之间用空格区分**

void zhuziduqu()
{
	ifstream fin("data.txt");
	string s;
	int count = 0;
	while (fin>>s)
	{
		cout << "Read from file: " << s << endl;
		count++;
	}
	cout << count << endl;
}

统计文件总行数
//读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分**

void zhuhangduqu()
{
	ifstream fin("data.txt");
	const int LINE_LENGTH = 100;
	char str[LINE_LENGTH];
	int count = 0;
	while (fin.getline(str, LINE_LENGTH))    //第二个参数,每行不超过这么多字符。
	{
		cout << "Read from file: " << str << endl;
		count++;
	}
        cout << count << endl;
}

4编写一程序,将C++源程序每行前加上行号与一个空格。

#include<iostream>
#include<fstream>
using namespace std;
int main() {
	int i = 1;
	char c[1000];
	ifstream ifile("1.txt");
	ofstream ofile("2.txt");
	while (!ifile.eof()) {
		ofile << i++ << ": ";
		ifile.getline(c, 999);
		ofile << c << endl;
	}
	ifile.close();
	ofile.close();
	cout << "读取完成" << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

4.5编写一程序,输出 ASCII码值从20到127的ASCII码字符表,格式为每行10个。

	#include<iostream>
using namespace std;
int main() {
	int i, l=0;
	for (i = 32; i < 127; i++) {
		cout << char(i) << " ";
		l++;
		if (l % 10 == 0)
		cout << endl;
	}
	cout << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值