IO流c++

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;
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值