char ch;
while ((ch = cin.get()) != EOF)
{
cout << ch << endl;
}
输入:aabbccdd,系统会把输入的内容放在缓冲区中,然后get函数,从缓冲区中一个一个的读取。
只要没有碰到EOF(ctrl+z),程序就会一直执行。
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,因为只从缓冲区取前三个,后边的就不取了。
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则算上空格一块取走。
代码:
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 << "hello" << endl;
cout.put('h').put('e').put('i');
输出结果:
hello
hei请按任意键继续. . .
代码如下:
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)以左对齐方式输出整数,域宽为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.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;
}
统计单词总数://读取方式: 逐词读取, 词之间用空格区分**
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;
}
#include
#include
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;
}
#include
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;
}