1 标准输入流函数
标准输入流对象cin,重点掌握的函数
cin.get(一个参数) //读一个字符 cin.get(三个参数) //可以读字符串 cin.getline() cin.ignore() cin.peek() cin.putback()
#include<iostream>
using namespace std;
void main() {
char mybuf[1024];
int myInt;
long myLong;
cin >> myInt;
cin >> myLong;
cin >> mybuf;
cout << "myInt: " << myInt << " myLong: " << myLong << " mybuf: " << mybuf << endl;
}
cin.get() //一次只能读取一个字符 输入EOF时(ctrl+z)结束, 回车从缓冲区输出字符
void main() {
char ch;
while ((ch = cin.get()) != EOF) {
cout << ch << endl;
}
}
结果
abcdef
a
b
c
d
e
f
^Z
缓冲区
void main() {
char a, b, c;
cout << "cin.get(a) 如果缓冲区没有数据,则程序阻塞\n";
cin.get(a);
cin.get(b);
cin.get(c);
cout << a << b << c << "因为缓冲区有数据,所以程序不会阻塞\n";
cin.get(a).get(b).get(c);
cout << a << b << c;
}
结果
cin.get(a) 如果缓冲区没有数据,则程序阻塞
abcdefg
abc因为缓冲区有数据,所以程序不会阻塞
def
getline
void main() {
char buf1[256];
char buf2[256];
cout << "请输入一个字符串含有多个空格 aa cc dd\n";
cin >> buf1;
cin.getline(buf2, 256);
cout << "buf1:" << buf1 << "\nbuf2:" << buf2 << endl;
}
结果
请输入一个字符串含有多个空格 aa cc dd
aacc bb aa ee
buf1:aacc
buf2: bb aa ee
ignore 忽略缓冲区数据
void main() {
char buf1[256];
char buf2[256];
cout << "请输入一个字符串含有多个空格 aa bbccdd\n";
cin >> buf1;
cin.ignore(2);
cin.getline(buf2, 256);
cout << "buf1:" << buf1 << "\nbuf2:" << buf2 << endl;
}
结果
请输入一个字符串含有多个空格 aa bbccdd
aa bbccdd
buf1:aa
buf2:bbccdd
peek 看缓冲区中的数据
void main() {
char buf1[256];
char buf2[256];
cout << "请输入一个字符串含有多个空格 aa bbccdd\n";
cin >> buf1;
cin.ignore(2);
int myint = cin.peek();
cout << "myint:" << myint << endl;
cin.getline(buf2, 256);
cout << "buf1:" << buf1 << "\nbuf2:" << buf2 << endl;
}
结果
请输入一个字符串含有多个空格 aa bbccdd
aa bde
myint:100
buf1:aa
buf2:de
案例:输入的整数和字符串分开处理
int main()
{
cout << "Please, enter a number or a word: ";
char c = std::cin.get();
if ((c >= '0') && (c <= '9'))
{
int n;
cin.putback(c);
cin >> n;
cout << "You entered a number: " << n << '\n';
}
else
{
string str;
cin.putback(c);
getline(cin, str);
cout << "You entered a word: " << str << '\n';
} system("pause");
return 0;
}
2 标准输出流函数
void main() {
cout << "hello" << endl;
cout.put('h').put('e').put('o').put('\n');
const char* p = "hello idcast";
cout.write(p, strlen(p)) << endl;
cout.write(p, strlen(p) - 4) << endl;
cout.write(p, strlen(p) + 4) << endl;
}
结果
hello
heo
hello idcast
hello id
hello idcast
#include<iostream>
#include<string>
#include <iomanip>
using namespace std;
void main() {
cout << "<start>";
cout.width(30);
cout.fill('*');
cout.setf(ios::showbase);
cout.setf(ios::internal);
cout << hex << 123 << "<End>\n";
cout << endl << endl;
cout << "<Start>"
<< setw(30)
<< setfill('*')
<< setiosflags(ios::showbase)
<< setiosflags(ios::internal)
<< hex
<< 123
<< "<End>\n"
<< endl;
}
结果
<start>0x**************************7b<End>
<Start>0x**************************7b<End>
3 文件I/O
1 文件读写
#include<iostream>
using namespace std;
#include<fstream>
void main() {
const char* fname = "1.txt";
ofstream fout(fname);
fout << "hello...1" << endl;
fout << "hello...2" << endl;
fout << "hello...3" << endl;
fout.close();
ifstream fin(fname);
char ch;
while (fin.get(ch)) {
cout << ch;
}
fin.close();
}
得到1.txt文件
2 二进制文件读写
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<fstream>
class Teacher {
private:
int age;
char name[32];
public:
Teacher() {
age = 33;
strcpy(name, "");
}
Teacher(int _age, const char* _name) {
age = _age;
strcpy(name, _name);
}
void printT() {
cout << "age: " << age << " name: " << name << endl;
}
};
void main() {
const char* fname = "2.dat";
ofstream fout(fname, ios::binary);
if (!fout) {
cout << "打开文件失败" << endl;
return;
}
Teacher t1(31, "t31"), t2(32, "t32");
fout.write((char*)& t1, sizeof(t1));
fout.write((char*)& t2, sizeof(t2));
fout.close();
ifstream fin(fname);
Teacher tmp;
fin.read((char*)& tmp, sizeof(Teacher));
tmp.printT();
fin.read((char*)& tmp, sizeof(Teacher));
tmp.printT();
fin.close();
}
|