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();//返回读指针位置

本博客作为一个入门者的自学记录,欢迎各位同行者不吝赐教。
原文地址:https://www.cnblogs.com/by-dxm/p/5452272.html