输入输出流(I/O stream)
#include <iostream>
using namespace std;
class cincout
{
public:
cincout(int a = 0, int b = 0)
{
c1 = a;
c2 = b;
}
void showc1c2(void)
{
cout << "c1=" << c1 << '\t' << "c2=" << c2 << endl;
}
friend istream& operator>>(istream&,cincout&);//在类中原型说明,创建一个友元函数,重载cincout类对象的>>输入运算符.
private:
int c1, c2;
};
istream& operator>>(istream& is, cincout& cc)//类外定义函数
{
is >> cc.c1 >> cc.c2;//重新定义输入流
return is;//返回输入流
}
int main()
{
cincout o1, o2;
o1.showc1c2();
o2.showc1c2();
cin >> o1;//从键盘输入
cin >> o2;
o1.showc1c2();
o2.showc1c2();
return 0;
}
#include <iostream>
using namespace std;
class incout
{
public:
incout(int a = 0, int b = 0)
{
c1 = a;
c2 = b;
}
void show(void)
{
cout << "c1=" << c1 << '\t' << "c2=" << c2 << endl;
}
friend istream& operator>>(istream&, incout&);//在类中原型说明,创建一个友元函数,重载cincout类对象的>>输入(提取)运算符.
friend ostream& operator<<(ostream&, incout&);//重载cincout类对象的<<输出(插入)运算符.
private:
int c1, c2;
};
istream& operator>>(istream& is, incout& cc)//类外定义函数
{
is >> cc.c1 >> cc.c2;//重新定义输入流
return is;//返回输入流
}
ostream& operator<<(ostream& os, incout& cc)
{
os << "c1=" << cc.c1 << '\t' << "c2=" << cc.c2 << endl;
return os;
}
int main()
{
incout obj1, obj2;
cout << obj1 << obj2 << endl;//调用输出函数
cin >> obj1;
cin >> obj2;
cout << obj1 << obj2;
return 0;
}
文件流
C++标准库提供了3各类用于实现文件操作,统称为文件流类
ifstream:专门用于从文件读取数据
ofstream:专门用于向文件写入数据
fstream:既可以从文件读取数据,又可用于向文件写入数据
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const char* str = "http://www.163.com";
fstream fs;//创建一个fstream类的对象
fs.open("demo.text", ios::out);//将demo.text文件和fs文件流建立关联
fs.write(str, 100);
fs.close();
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//ifstream infile;
//infile.open(".\\demo.txt", ios::in);
//if (infile)//条件成立,说明文件打开成功
//{
// cout << "\ndemo.txt文件打开成功." << endl;
// infile.close();
//}
//else
//{
// cout << "\ndemo.txt文件打开失败." << endl;
// return 1;
//}
//ofstream outfile;
//outfile.open(".\\outdemo.text", ios::out);
//if (outfile)//条件成立,说明文件打开成功
//{
// cout << "\noutdemo.txt文件打开成功." << endl;
// outfile.close();
//}
//else
//{
// cout << "\noutdemo.txt文件打开失败." << endl;
//}
fstream iofile;
iofile.open(".\\iodemo.text", ios::in|ios::out|ios::trunc);
if (iofile)//条件成立,说明文件打开成功
{
cout << "\niodemo.txt文件打开成功." << endl;
iofile.close();
}
else
{
cout << "\niodemo.txt文件打开失败." << endl;
}
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
class student
{
public:
int no;
char name[10];
int age;
};
int main()
{
student stu;
ofstream outfile("student.dat", ios::out | ios::binary);
if (!outfile)
{
cout << "\n打开文件student.dat失败" << endl;
}
else
{
cout << "\n打开文件student.dat成功" << endl;
}
while (cin >> stu.no >> stu.name >> stu.age)//从键盘输入
outfile.write((char*)&stu, sizeof(stu));
outfile.close();
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
class student
{
public:
int no;
char name[10];
int age;
};
int main()
{
student stu;
ifstream infile("student.dat", ios::in | ios::binary);//以二进制方式打开文件
if (!infile)
{
cout << "\n打开文件student.dat失败" << endl;
return 0;
}
else
{
cout << "\n打开文件student.dat成功" << endl;
}
while (infile.read((char*)&stu, sizeof(stu)))
{
cout << stu.no << "," << stu.name << "," << stu.age << endl;
}
infile.close();
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char ch;
ofstream outfile("outdemo.txt", ios::out | ios::binary);
if (!outfile)
{
cout << "\noutdemo.txt文件打开失败." << endl;
return 0;
}
else
cout<< "\noutdemo.txt文件打开成功." << endl;
while (cin >> ch)
{
outfile.put(ch);
}
outfile.close();
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char ch;
ifstream infile("outdemo.txt", ios::out | ios::binary);
if (!infile)
{
cout << "\noutdemo.txt文件打开失败." << endl;
return 0;
}
else
cout << "\noutdemo.txt文件打开成功." << endl;
while ((ch = infile.get()) && ch != EOF)
{
cout << ch;
}
infile.close();
return 0;
}
//文件指针
//随机读取二进制文件
//infile.seekg(int);//将文件指针移动到由参数指定的字节处
//infile.seekg(100);//将文件指针移动到距离文件头100个字节处
//infile.seekg(int,ios::_dir);
//_dir
//beg:文件头
//cur:当前位置
//end:文件尾
//infile.seekg(100,ios::beg);移动到距文件头100个字节
//infile.seekg(-100,ios::cur);移动到距当前位置前100个字节
//infile.seekg(-500,ios::end);移动到距文件尾前500个字节