IO流类库
输入输出流
#include <iostream>
using namespace std;
class InCount
{
public:
InCount(int a = 0, int b = 0)
{
c1 = a;
c2 = b;
}
void show(void)
{
cout << "c1=" << c1 << "\t" << "c2=" << c2 << endl;
}
friend istream& operator>>(istream&, InCount&);
friend ostream& operator<<(ostream&, InCount&);
private:
int c1, c2;
};
istream& operator>>(istream& is, InCount& cc)
{
is >> cc.c1 >> cc.c2;
return is;
}
ostream& operator<<(ostream& os, InCount& cc)
{
os << "c1=" << cc.c1 << "\t" << "c2=" << cc.c2 << endl;
return os;
}
int main()
{
InCount obj1, obj2;
cout << obj1 << obj2 << endl; // 调用输出函数
cin >> obj1;
cin >> obj2;
cout << obj1 << obj2;
return 0;
}
#include<iostream>
using namespace std;
#include<fstream>
int main()
{
string s = "https://www.baidu.com";
fstream fs;
fs.open("file.txt", ios::out);
fs.write(s.c_str(), s.size());
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.txt", ios::out);
//if (outFile) // 条件成立,则说明文件打开成功
//{
// cout << "\noutdemo.txt文件打开成功." << endl;
// outFile.close();
//}
//else
//{
// cout << "\noutdemo.txt文件打开失败." << endl;
//}
fstream ioFile;
ioFile.open(".\\iodemo.txt", 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.txt失败." << endl;
outFile.close();
}
else
{
cout << "\n打开文件student.txt成功." << 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打开失败." << endl;
return 0;
}
else
cout << "\nstudent.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;
}
例子
// 009.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#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;
}