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


相关推荐

  1. <span style='color:red;'>IO</span><span style='color:red;'>流</span><span style='color:red;'>c</span>++

    IOc++

    2024-04-05 01:36:01      39 阅读
  2. IO

    2024-04-05 01:36:01       69 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 01:36:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 01:36:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 01:36:01       87 阅读
  4. Python语言-面向对象

    2024-04-05 01:36:01       96 阅读

热门阅读

  1. 关于在PyCharm中使用虚拟环境

    2024-04-05 01:36:01       39 阅读
  2. GCC -Wl参数详解

    2024-04-05 01:36:01       39 阅读
  3. 如何通过pciehp管理NVMe SSD电源状态?

    2024-04-05 01:36:01       78 阅读
  4. 英国和美国签署开发人工智能安全测试协议

    2024-04-05 01:36:01       37 阅读
  5. Python语言例题集(010)

    2024-04-05 01:36:01       32 阅读