C语言
主页 > 软件编程 > C语言 >

C++文件IO流及stringstream流读写文件和字符串操作介绍

2023-11-22 | 佚名 | 点击:

一、引入

1

2

3

4

5

6

7

8

9

int main()

{

    string str;

    while (cin >> str)

    {

        cout << str << endl;

    }

    return 0;

}

我们在OJ的时候经常会用到while(cin >> str),这里的流提取实际上是个阻塞操作,只要缓冲区还有数据就继续读,默认以空格或者换行结束,有空格说明是把两段字符串尾插到str。

那么它是怎么结束呢?

答案是输入[Ctrl]-c或者[Ctrl]-z + 换行。

[Ctrl]-c是发送信号结束进程。

[Ctrl]-z + 换行是通过返回值条件判断结束while循环,具体看下面讲解。

二、自定义类型隐式类型转换

cin >> str的返回值是一个istream类

实际上返回的就是cin对象。而c++98支持了隐式类型转换,把istream转换为bool,所以能够条件判断。

具体是怎么转换的呢?

看下面这个例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

class A

{

public:

    A(int a)

        : _a(a)

    {}

private:

    int _a;

};

int main()

{

    // 内置类型转换成自定义类型

    A a = 1;

    return 0;

}

这里按道理来说是构造一个临时对象再拷贝构造,而编译器优化成了直接构造。如果没有单参数的构造函数就无法转换。

那如果我们想要让自定义类型转换成内置类型呢?

直接int aa = a;肯定会报错。

但是我们可以加一个特殊的重载函数。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

class A

{

public:

    A(int a)

        : _a(a)

    {}

    operator int()

    {

        return _a;

    }

private:

    int _a;

};

int main()

{

    // 内置类型转换成自定义类型

    A a = 1;

    // 自定义类型转化成内置类型

    int aa = a;

    cout << aa << endl;

    return 0;

}

而我们上面说的把istream转化成bool类型就是类似这样实现的。

operator bool() 里面会检查是特殊字符([Ctrl]-z )就会返回false。

三、sync_with_stdio同步

我们知道cin和scanf都有自己的缓冲区,而如果我们用scanf写入再用cout输出,就会导致速度变慢很多(缓冲区拷贝)。

而sync_with_stdio函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑到了一起。

决定C++标准streams(cin,cout,cerr…)是否与相应的C标准程序库文件(stdin,stdout,stderr)同步,也就是是否使用相同的stream缓冲区,缺省情况是同步的,但由于同步会带来某些不必要的负担,因此该函数作用就是我们自己可以取消同步 。

1

2

3

4

5

6

7

#include <iostream>

int main()

{

    std::ios::sync_with_stdio(false);

    std::cin.tie(0);

    // IO

}

四、文件IO流

文件的读写有两种:

1?? 二进制读写

2?? 文本读写

ofstream是写入文件,而ifstream是从文件中读取。

4.1 open和close文件

这里的参数表示我们想以什么样的方式打开文件。

比方说当我们想用二进制的方式打开文件:

ofs.open ("test.txt", std::ofstream::out | std::ofstream::binary)

而我们也可以在构造的时候直接传进参数。

ofstream ofs("test.txt", std::ios_base::out | std::ios_base::binary)

4.2 写入文件与读出文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

struct ServerInfo

{

    char _address[32];

    int _port;

};

struct Config

{

public:

    Config(const char* filename)

        : _filename(filename)

    {}

    void Write(ServerInfo info)

    {

        ofstream ofs("test.txt", std::ios_base::out | std::ios_base::binary);

        ofs.write((char*)&info, sizeof info);

    }

    void Read(ServerInfo& info)

    {

        ifstream ifs("test.txt", std::ios_base::in | std::ios_base::binary);

        ifs.read((char*)&info, sizeof info);

    }

private:

    string _filename;

};

int main()

{

    Config con("text.txt");

    ServerInfo si = { "aaaaaa", 910 };

    con.Write(si);

    return 0;

}

而我们也可以把数据读回来。

1

2

3

4

5

6

7

8

9

10

int main()

{

    Config con("text.txt");

    //ServerInfo si = { "aaaaaa", 910 };

    //con.Write(si);

    ServerInfo si;

    con.Read(si);

    cout << si._address << " " << si._port << endl;

    return 0;

}

可以看到内存中和写出去显示出来的不一样。

当然我们可以用文本读写的方式。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

struct ServerInfo

{

    char _address[32];

    int _port;

};

struct Config

{

public:

    Config(const char* filename)

        : _filename(filename)

    {}

    void Write(ServerInfo info)

    {

        ofstream ofs(_filename);

        // 重载

        ofs << info._address << endl;

        ofs << info._port << endl;

    }

    void Read(ServerInfo& info)

    {

        ifstream ifs(_filename);

        // 重载

        ifs >> info._address;

        ifs >> info._port;

    }

private:

    string _filename;

};

int main()

{

    Config con("text.txt");

    ServerInfo si = { "aaaaaa", 910 };

    con.Write(si);

    /*ServerInfo si;

    con.Read(si);

    cout << si._address << " " << si._port << endl;*/

    return 0;

}

五、stringstream流的使用

在程序中如果想要使用stringstream,必须要包含头文件。在该头文件下,标准库三个类:

istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作。

5.1 将数值类型数据格式化为字符串

1

2

3

4

5

6

7

8

9

10

11

12

int main()

{

    int a = 123;

    const char* b = "456";

    double c = 78.9;

    ostringstream os;

    os << a;

    os << b;

    os << c;

    cout << os.str() << endl;

    return 0;

}

当然我们也可以把每个数据都提取出来。但此时输入的时候就要空格或者换行隔开。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

int main()

{

    int a = 123;

    const char* b = "456";

    double c = 78.9;

    ostringstream os;

    os << a << " ";

    os << b << " ";

    os << c << " ";

    string ret = os.str();

    cout << ret << endl;

    int d;

    char e[20];

    double f;

    istringstream is(ret);

    is >> d >> e >> f;

    cout << d << " ";

    cout << e << " ";

    cout << e << " ";

    return 0;

}

5.2 序列化和反序列化

序列化指的是将一个内存对象转化成一串字节数据(存储在一个字节数组中),可用于保存到本地文件或网络传输。反序列化就是将字节数据还原成内存对象。

总结

序列化:将对象变成字节流的形式传出去。

反序列化:从字节流恢复成原来的对象。

简单来说,对象序列化通经常使用于两个目的:

1?? 将对象存储于硬盘上,便于以后反序列化使用;

2?? 在网络上传送对象的字节序列

我们现在模拟一个聊天的发送窗口。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

class Date

{

    friend ostream& operator << (ostream& out, const Date& d);

    friend istream& operator >> (istream& in, Date& d);

public:

    Date(int year = 1, int month = 1, int day = 1)

        :_year(year)

        , _month(month)

        , _day(day)

    {}

private:

    int _year;

    int _month;

    int _day;

};

istream& operator >> (istream& in, Date& d)

{

    in >> d._year >> d._month >> d._day;

    return in;

}

ostream& operator << (ostream& out, const Date& d)

{

    out << d._year << " " << d._month << " " << d._day;

    return out;

}

struct ServerInfo

{

    friend istream& operator >> (istream& in, ServerInfo& si);

    friend ostream& operator << (ostream& out, ServerInfo& si);

    string _name;// 昵称

    Date _d;// 时间

    string _msg;// 信息

};

istream& operator >> (istream& in, ServerInfo& si)

{

    in >> si._name  >> si._d >> si._msg;

    return in;

}

ostream& operator << (ostream& out, ServerInfo& si)

{

    out << si._name << " ";

    out << si._d << " ";

    out << si._msg << " ";

    return out;

}

int main()

{

    ServerInfo p{ "海阔天空", {2023, 4, 19}, "hello" };

    stringstream os;

    os << p;

    string ret = os.str();

    ServerInfo is;

    stringstream oss(ret);

    oss >> is;

    cout << "-------------------------------------------------------" << endl;

    cout << "昵称:" << is._name << " ";

    cout << is._d << endl;

    cout << is._name << ": " << is._msg << endl;

    cout << "-------------------------------------------------------" << endl;

    return 0;

}

原文链接:
相关文章
最新更新