文件操作

文件分为普通文件和二进制文件

普通文件可以直接打开不需要经过特定的软件   字符流

二进制文件 需要特定的软件  字节流

c++通过fstream头文件进行文件的调用

文本文件一般使用 get put 进行读写

二进制文件利用 write和read

打开关闭open  close   is_open

读写文件 get/put  read/write  getline 输入输出运算符

操作 seekp 移动文件指针的位置
    tellp  得到文件指针的偏移量

  1 #include<iostream>
  2 #include<fstream>//文件的头文件用于对文件对象的操纵
  3 #include<iomanip>// 作用看控制cout的输出 让文件可以通过<<进行输出
  4 using namespace std;
  5 class A
  6 {
  7 public:
  8     virtual void showData() = 0;//纯虚函数
  9 };
 10 class B :public A//类B继承类A
 11 {
 12     int data;//类B中设置的新成员
 13 public:
 14     void setData()
 15     {
 16         data = 0;
 17     }//类B中使用的新函数
 18     void showData()//父类指针的调用
 19     {
 20         setData();
 21         cout << data << endl;//利用虚函数调用子类中的函数和变量
 22     }
 23 };
 24 class C :public A//类C继承类A 利用类A中的纯虚函数重写showData方法
 25 {
 26 public:
 27     void showData()
 28     {
 29         //新的功能
 30     }
 31 
 32 };
 33 void fun(A*p)
 34 {
 35     p->showData();//不管函数里写了什么 主要是父类指针调用虚函数
 36 }
 37 int main()
 38 {
 39 #if 0
 40     fun(new B);//调用B的虚函数
 41     fun(new C);//调用C的虚函数
 42     fstream file;//文件对象 所有对文件对象的操作全部实现了文件对象的成员函数
 43     file.open("1.text", ios::out | ios::in);
 44     //is::in只读  文件不存在打开失败  is::out只写打开文件不存在会创建 存在会清空里面的内容
 45     //is::app追加  ios::binary二进制  组合的时候用|连接
 46     if (file.is_open())//判断文件是否打开
 47     {
 48         cout << "文件打开成功" << endl;
 49     }
 50     else
 51     {
 52         cout << "文件打开失败" << endl;
 53         cin.get();
 54         return 0;
 55     }//file.is_open为false的时候 文件打开失败 结束程序return 0
 56     char ch = 'A';
 57     file.put(ch);//将字符A写入到文档中
 58     file.get(ch);//读取文件中的一个字符
 59     ch = file.get();
 60     char str[128];
 61     file.get(str, 128);//从文件中读取一个字符串长度不超过128
 62     //128控制长度 防止越界
 63     file.get(str, 128, ' ');//比上面多了一个限制条件 遇到空格停止读取
 64     file.read(str, 128);//以字节为单位进行读取
 65     //表示从文件中读取128个字节内容 放到str中
 66     int x;
 67     file.read((char*)&x, sizeof(int));//从文件中读取一个int 放到x中
 68     //这里将int的x强转成char类型 之后的x都是char类型
 69     file.write((char*)&x, sizeof(int));
 70     file.write(str, 128);//在文件中写入一个字符串长度不超过128
 71     file.getline(str, 128);//与get相似但是getline是活的行数
 72     file.getline(str, 128, ' ');
 73     //cin和cout输入和输出运算符
 74     file >> x;//用文件内容给x赋值 读文件
 75     file << x;//用文件内容输出x的值 写文件
 76     streamoff off = file.tellp();//操作读指针
 77     file.seekp(0, ios::end);
 78     file.tellg();//操作写指针 seekg
 79         /*
 80         is::beg 文件开头
 81         is::cur 当前位置
 82         is::end 文件结尾
 83 
 84         */
 85     file.close();//关闭文件
 86 #endif;
 87     int num = 20;
 88     //hex 16进制 oct 8进制 dec 十进制
 89     cout << hex << num << endl;//十六进制的输出num
 90     cout << setw(5) << setfill('A') << num << endl;
 91     //setw至少输出5为 setfill不足用A补齐 默认右对齐输出结构为 AAA14
 92     cout <<setiosflags(ios::left)<< setw(5) << setfill('A') << num << endl;
 93     //setiosflags(ios::left)左对齐 left向左 结果为14AAA
 94     float weight = 3.141592654f;
 95     cout << weight << endl;
 96     cout << setprecision(10) << resetiosflags(ios::fixed) << weight << endl;
 97     //setprecision(10)设置10位小数 resetiosflags(ios::fixed)强制小数形式表示
 98     //关于输入
 99         char ch = cin.get();//设置一个字符返回ch
100         cin.get(ch);
101         char str[128];//设置一个长度为128的字符串
102         cin.get(str, 128);
103         cin.get(str, 128, ' ');
104 
105         cin.get();
106         return 0;
107 
108 
109 }
原文地址:https://www.cnblogs.com/liugangjiayou/p/11386594.html