zoukankan      html  css  js  c++  java
  • 17.文件操作

    17.1 文件流类

    17.2 文件的打开关闭

     #include<fstream>

     fstream oFile("D:\dang.dat",ios::out|ios::binary); 或 fstream oFile;

                                                                                 oFile.open("D:\......",ios::out|ios::binary);

    检查是否成功打开  

     if(!oFile)

      cout<<"oFile open error"<<endl;

    17.3 文件的读写

    (1)文本文件的读写

    #include<iostream>
    #include<fstream>
    using namespace std;
    char *a;
    int main(){
        ofstream oFile("D:\dang.txt",ios::out);
        if(!oFile)
        cout<<"ofile open error";
        char p[10];
        for(int i=0;i<5;i++)
        cin>>p[i];
        for(int i=0;i<5;i++)
        oFile<<p[i];
        oFile.close();
        
        ifstream iFile("D:\dang.txt",ios::in);
        if(!iFile)
        cout<<"ifile open error";
        char str[10];
        iFile.getline(str,10);
        cout<<str<<endl;
        iFile.close();
        
        return 0;
    }

    (2)二进制文件读写

         write和read

       *   ostream write(const char * buffer,int count);

            继承自ostream,将内存中buffer指向的count个字节写入文件

       **  istream read(char * buffer,int count);

             继承自iostream,从文件中读取count个字节内容存放到buffer指向的内存缓冲区中

    #include<iostream>
    #include<fstream>
    #include<cstring>
    
    using namespace std;
    
    class student{
        public:
            char name[20]; 
            int age;
    }; 
    
    int main() {
        //写入几个对象至文件 
        student stu;
        fstream oFile("erjinzhi.dat",ios::out|ios::binary);
        if(!oFile) 
        cout<<"oFile open error";
        while(cin>>stu.name>>stu.age){ 
          if(strcmp(stu.name,"end")==0) break; 
            oFile.write( (char *) & stu, sizeof(stu) );
            } 
           oFile.close();
           
         //读出刚刚写入的文件  
        fstream iFile("erjinzhi.dat",ios::in|ios::binary);
        if(!iFile)
        cout<<"iFile open error";
        while(iFile.read((char*)&stu,sizeof(stu)))
        cout<<stu.name<<" "<<stu.age<<endl;
        iFile.close();
        
        //改变第三个对象的名字,再读出文件 
        fstream make_File("erjinzhi.dat",ios::in|ios::out|ios::binary);
        if(!make_File)
        cout<<"make_FIle open error";
        make_File.seekp(2*sizeof(stu),ios::beg);//定位写指针 
        make_File.write("Make",sizeof("Make")+1);
        make_File.seekg(0,ios::beg);//定位读指针 
        while(make_File.read((char*)&stu,sizeof(stu)))
        cout<<stu.name<<" "<<stu.age<<endl;
        make_File.close();
        
           
        return 0; 
    }

       文件流类put和get成员函数读写(文件拷贝)

        在控制台里面执行

    #include<iostream>
    #include<fstream>
    using namespace std;
    int main(int argc,char *argv[]){
        if(argc!=3){
            cout<<"File name missing!"<<endl;
            return 0;
        }
        
        ifstream iFile(argv[1],ios::binary|ios::in);//打开文件用于读
        if(!iFile){
        cout<<"iFile open error"<<endl;
        return 0;    
        }
        ofstream oFile(argv[2],ios::binary|ios::in);
        if(!oFile){
        cout<<"oFile open error"<<endl;
        iFile.close();
        return 0;    
        }
    
        
        char c;
        while(iFile.get(c))
            oFile.put(c);
            
        oFile.close();
        iFile.close();    
        
        return 0;
    } 

    17.4 操作文件读写指针

         ostream & seekp(int offset,int mode);

         istream & seekg(int offset,int mode);

    *  mode 代表读写指针的模式

        ios::beg   从文件开始向后移动offset字节,offset为正或0

        ios::cur    从当前位置向后(或前)移动offset个字节,offset正负数,0皆可

        ios::end   从文件结尾向前offset字节,offset只能是正数或0

    ** 得到读写指针位置

        int tellp();//返回写指针位置

        int tellp();//返回读指针位置

    本博客作为一个入门者的自学记录,欢迎各位同行者不吝赐教。
  • 相关阅读:
    子类构造函数中调用虚函数问题验证
    socks5代理浅识
    关于C++标准库(第2版)std::remove_if的"特性"概述
    动态获取结构体中指定的属性值
    构造和析构函数定义为私有场景
    remove_pointer使用测验
    广播自定义消息实现进程间的通信问题
    遍历窗口权限问题
    嵌入窗口到桌面的问题
    实验一 熟悉实验环境
  • 原文地址:https://www.cnblogs.com/by-dxm/p/5452272.html
Copyright © 2011-2022 走看看