标准输入流
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
void test1()
{
int myInt;
long myLong;
char buf[128] = { 0 };
cin >> myInt;
cin >> myLong;
cin >> buf;
cout << "myInt: " << myInt << endl;
cout << "myLong: " << myLong << endl;
cout << "buf: " << buf << endl;
}
void test2()
{
char ch;
while ((ch = cin.get() )!= EOF) {
cout << ch << endl;
}
}
void test3()
{
char a, b, c;
char buf[10] = { 0 };
cout << "从输入缓冲区去读取数据,如果缓冲区中没有数据,就阻塞" << endl;
cin.get(buf, 10, ' ');
cout << buf << endl;
}
void test4()
{
char buf[128] = { 0 };
cout << "请输入一个字符串 aa bb cc dd" << endl;
cin.getline(buf, 128);
cout << "buf:" <<buf << endl;
}
void test5()
{
char buf1[128];
char buf2[128];
cout << "请输入一个字符串 aa bb cc dd" << endl;
cin >> buf1;
cin.ignore(2);
cin.getline(buf2, 128);
cout << "buf1:" << buf1 << endl;
cout << "buf2:" << buf2 << endl;
}
void test6()
{
cout << "请输入一个数字或者字符串" << endl;
char ch;
ch = cin.get();
if ((ch >= '0') && ch <= '9') {
cout << "输入的是一个数字" << endl;
int num;
cin.putback(ch);
cin >> num;
cout << "num =" << num << endl;
}
else {
cout << "输入的是一个字符串" << endl;
char buf[128] = { 0 };
cin.getline(buf, 128);
cout << "buf:" << buf << endl;
}
}
int main(void)
{
test6();
return 0;
}
标准输出流
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
using namespace std;
void test1()
{
cout << "hello " << endl;
cout.put('h').put('e').put('l') << endl;
char str[] = "hello wolrd";
cout.write(str, strlen(str));
cout << endl;
cout.write(str, strlen(str) - 1);
cout << endl;
}
void test2()
{
cout << "<Start>"
<< setw(30)
<< setfill('*')
<< setiosflags(ios::showbase)
<< setiosflags(ios::internal)
<< hex
<< 123
<< "<End>\n"
<< endl;
}
void test3()
{
#if 0
int a;
cout << "input a:";
cin >> a;
cout << "dec:" << dec << a << endl;
cout << "hex:" << hex << a << endl;
cout << "oct:" << setbase(8) << a << endl;
const char *pt = "China";
cout << setw(10) << pt << endl;
cout << setfill('*') << setw(10) << pt << endl;
#endif
double pi = 22.0 / 7.0;
cout << setiosflags(ios::scientific) << setprecision(8);
cout << "pi=" << pi << endl;
cout << "pi=" << setiosflags(ios::fixed) << pi << endl;
}
void test4()
{
double a = 123.456, b = 3.14159, c = -3214.67;
cout << setiosflags(ios::fixed) << setiosflags(ios::right) << setprecision(2);
cout << setw(10) << a << endl;
cout << setw(10) << b << endl;
cout << setw(10) << c << endl;
}
int main(void)
{
test4();
return 0;
}
C++ 文件流的基本操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
using namespace std;
void test_write_to_file()
{
char file_name[] = "c:/test/hahaha.txt";
ofstream fout(file_name, ios::app | ios::out);
if (!fout) {
cout << "打开文件失败" << endl;
}
fout << "hello...111" << endl;
fout << "hello...222" << endl;
fout << "hello...333" << endl;
fout.close();
}
void test_read_from_file()
{
char file_name[] = "c:/test/hahaha.txt";
ifstream fin(file_name, ios::in);
if (!fin) {
cout << "打开文件失败" << endl;
}
char ch;
while (fin.get(ch)) {
cout << ch;
}
fin.close();
}
class Teacher
{
public:
Teacher() {
}
Teacher(int id, const char name[])
{
this->id = id;
strcpy(this->name, name);
}
void printT() {
cout << "id = " << id << endl;
cout << "name = " << name << endl;
}
private:
int id;
char name[32];
};
void test_write_binary_to_file()
{
char file_name[] = "c:/test/bin_teacher.dat";
Teacher t1(1001, "zhang3");
Teacher t2(1002, "li4");
ofstream fout(file_name, ios::binary);
if (!fout) {
cout << "打开文件失败" << endl;
return;
}
fout.write((char*)&t1, sizeof(Teacher));
fout.write((char*)&t2, sizeof(Teacher));
fout.close();
}
void test_read_binary_from_file()
{
char file_name[] = "c:/test/bin_teacher.dat";
Teacher temp1;
Teacher temp2;
ifstream fin(file_name, ios::binary);
if (!fin) {
cout << "打开文件失败" << endl;
return;
}
fin.read((char*)&temp1, sizeof(Teacher));
temp1.printT();
fin.read((char*)&temp2, sizeof(Teacher));
temp2.printT();
fin.close();
}
int main(void)
{
test_read_binary_from_file();
return 0;
}