C++IO流(2)

简介: C++IO流(2)

字符流

包含头文件 sstream

1.isstringstream

2.ostringstream

3.stringstream

一般处理字符流的时候,使用stringstream类型的对象

获取字符流的string

string str(); //获取string

void str(const string & str); //重置流对象的字符串

字符流的作用

1.数据类型的转换

2.数据分割

数据类型的转换

#include<iostream>
#include<sstream>
using namespace std;
int main()
{
  char str[20];
  stringstream MM("温柔了岁月");
  MM >> str;
  cout << str;
  cout << MM.str() << endl;
  MM.str(""); //清除
  //整数转变成字符型 to_string()强转
  string buf = to_string(124);
  int number = 1203;
  char arr1[29];
  stringstream arr2(arr1);
  arr2 << number;
  arr2 >> arr1;
  cout << arr1;
  //字符型转整数
  stringstream arr3("123");
  int number2 = 0;
  arr3 >> number2;
  cout << number2 << endl;  
}

数据的分割

实现数据的分割

在流中,空格作为单一数据的间隔

#include<iostream>
#include<sstream>
#include<iomanip>
using namespace std;
int main()
{
  //数据分割(流中默认空格  作为单一数据的间隔)
  stringstream str1("ip: 192.168.1.1");
  char arr1[20];
  str1 >> arr1;
  char str2;
  int number[4];
  str1 >> number[0];
  str1 >> str2;
  str1 >> number[1];
  str1 >> str2;
  str1 >> number[2];
  str1 >> str2;
  str1 >> number[3];
  for (auto& v : number)
  {
    cout << v << setw(10);
  }
  system("pause");
  return 0;
}

同一个流在做转换的时候,必须要调用

clear清除。

文件流

头文件 fstream(可读可写)

1.ofstream 打开文件,只能写操作

2.ifstream 打开文件,只能读操作

一般大家创建一个fstream对象,可读可写

打开文件

1.构造的方式,带参数构造函数,const char * UR,ios :: openmode mode

2.成员函数方式:void open (char * URL, ios :: openmode mode)

读写方式 作用

ios::in 读的方式,打开文件

ios :: out 写的方式,打开文件

ios :: app 追加的方式,写文件

ios:: ate 打开已有文件,指针在文件末位

ios :: trunc 文件不存在,具有创建方式

ios :: binary 二进制打开,默认打开方式ASCII码

ios :: nocreat 不创建

ios :: noreplace 不代替

注意:组合的方式是用位或,例如:可读可写 ios::in | ios:: out

判断文件是否打开成功

1.!is_open()函数判断是否打开成功,!is_open()是1 :表示打开失败

2. !文件对象 ,结果是1 ,表示打开失败。

关闭文件

close 关闭文件

include<fstream>
using namespace std;
int main()
{
  fstream file("test.text", ios::in | ios::out | ios::trunc);
  //等效下面两行代码
  fstream file2;
  file2.open("text", ios::in | ios::out | ios::trunc);
  //判断文件打开方法一
  if (!file)
  {
    cerr << "打开文件失败" << endl;
  }
  //判断文件打开方法二
  if (!file.is_open())
  {
    cerr << "打开失败" << endl;
  }
  system("pause");
  return 0;
}

文件的读写

1.直接采用 >> 和 << 运算符,进行操作

2.采用成员函数进行读写

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
class MM
{
public:
  MM(){}
  MM(string name,int age,string sex): name(name),age(age),sex(sex) {}
  void print()
  {
    cout << name << setw(5) << age << setw(5) << sex << endl;
  }
  void Savefile(string filename)
  {
    fstream file(filename, ios::in | ios::out | ios::trunc);
    if (!file)
    {
      cerr << "打开文件失败" << endl;
    }
    file << name << setw(5) << age << setw(5) << sex << endl;
    file.close();
  }
  void read(string filename)
  {
    fstream file(filename, ios::in);
    if (!file)
    {
      cerr << "打开文件失败" << endl;
      return;
    }
    file >> this->name >> this->age >> this->sex;
    file.close();
  }
private:
  string name;
  int age;
  string sex;
};
int main()

用字符流的方式,将一个文件的内容,写到另一个文件里去

void charway(string readfile, string writefile)
  {
    fstream read1(readfile, ios::in);
    fstream write1(writefile, ios::out);
    while (!read1.eof())  //eof()字符的末尾
    {
      char key = read1.get();
      write1.put(key);
    }
    read1.close();
    write1.close();
  }

用二进制的方式读写文件

void bianry(string readfile, string writefile)
  {
    fstream read(readfile, ios::in | ios::binary);
    fstream write(writefile, ios::out | ios::binary);
    while (!read.eof())
    {
      char arr1[200];
      read.read(arr1, 200);
      write.write(arr1, 200);
    }
    read.close();
    write.close();
  }

文件指针的移动

//文件指针移动 -->计算文件的大小
  int test(string file)
  {
    fstream read(file, ios::in | ios::binary);
    read.seekg(0, ios::end);
    int size = read.tellg();
    read.close();
    return size;  
  }
相关文章
|
2月前
|
C语言 iOS开发 C++
10. C++IO流
10. C++IO流
24 0
|
1月前
|
C++
C++的简单文本文件IO
C++的简单文本文件IO
|
2月前
|
算法 C语言 C++
【C++】C++的IO流
【C++】C++的IO流
|
2月前
|
存储 算法 C语言
【C++入门到精通】C++的IO流(输入输出流) [ C++入门 ]
【C++入门到精通】C++的IO流(输入输出流) [ C++入门 ]
40 0
|
26天前
|
Linux C++
c++高级篇(三) ——Linux下IO多路复用之poll模型
c++高级篇(三) ——Linux下IO多路复用之poll模型
|
26天前
|
缓存 监控 网络协议
c++高级篇(二) ——Linux下IO多路复用之select模型
c++高级篇(二) ——Linux下IO多路复用之select模型
|
2月前
|
存储 算法 C语言
从C语言到C++_38(C++的IO流+空间适配器)STL六大组件联系(下)
从C语言到C++_38(C++的IO流+空间适配器)STL六大组件联系
34 5
|
2月前
|
C++ 数据格式
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
|
2月前
|
算法 C语言 C++
从C语言到C++_38(C++的IO流+空间适配器)STL六大组件联系(上)
从C语言到C++_38(C++的IO流+空间适配器)STL六大组件联系
28 0
|
2月前
|
安全 编译器 C语言
【C++高阶(九)】C++类型转换以及IO流
【C++高阶(九)】C++类型转换以及IO流