七、C++ファイル操作


C++ファイル操作
一、基本概念
データがコンピュータに保存される単位はビット(bit)、8ビットは1バイト(byte)を構成し、いくつかのバイトは1つの記録/ドメインを構成し、例えば学生の記録:
int ID;
char name[10];
int age;
int rank[10];

すべてのレコードを1つのファイルに順番に書き込み、生成されたファイルを順番ファイルと呼びます.C++標準ライブラリでは、ifstream、ofstream、fstreamの3つのクラスがファイル操作に使用され、ファイルストリームクラスと総称されます.
二、順序ファイルの作成
ファイル操作の基本的な流れは,1)ファイルを開くこと,2)ファイルを読む/書くこと,3)ファイルを閉じることである.
作成順序ファイルのコード:
#include <fstream> //      
ofstream outFile("clients.dat", ios::out|ios::binary); //    
  • ofstreamはfstreamで定義されたクラスで、outFileはofstreamオブジェクト
  • です.
  • 「clients.dat」は、作成するファイルのファイル名
  • です.
  • ios::outはファイルに出力されたときに、元の内容を削除することを指す.ios::appに置き換えると、ファイルに出力時に既存のコンテンツを保持し、ファイルの末尾にデータ
  • を追加することを意味する.
  • ios::binary、ファイル
  • をバイナリファイル形式で開くことを指す
    ofstreamオブジェクトを作成してopen関数で開くこともできます
    ofstream fout;
    fout.open( “test.out”, ios::out|ios::binary );

    オープンが成功したかどうかを判断するには、次の手順に従います.
    if(!fout) { cerr << “File open error!”<<endl; }

    ファイル名は絶対パスを与えることもできるし、相対パスを与えることもできる.パス情報が説明されていません.現在のフォルダの下でファイルを探します.
    三、ファイルの読み書きポインタ
    ofstream fout(“a1.out”, ios::app);
    long location = fout.tellp(); //        
    location = 10L;
    fout.seekp(location); //         10    
    fout.seekp(location, ios::beg); //   location
    fout.seekp(location, ios::cur); //      location
    fout.seekp(location, ios::end); //    location
    
    ifstream fin(“a1.in”,ios::in);
    long location = fin.tellg(); //        
    location = 10L;
    fin.seekg(location); //        10    
    fin.seekg(location, ios::beg); //   location
    fin.seekg(location, ios::cur); //      location
    fin.seekg(location, ios::end); //    location
    
    //location      

    四、バイナリファイルの読み書き
    このプログラムの例では、読み書きデータはStudioオブジェクトです.
    class Student {
        public:
            char name[20];
            int age;
    };

    4.1ファイルの書き込み
    
    void writeFile()
    {
        Student s;
        ofstream outfile("student.dat", ios::out|ios::binary);
    
        while(cin>>s.name>>s.age){
            if(stricmp(s.name, "exit") == 0)
                break;
            outfile.write((char *)&s, sizeof(s));
        }
    
        outfile.close();
    }
    

    4.2ファイルを読む
    void readFile()
    {
        Student s;
        ifstream infile;
        infile.open("student.dat", ios::in|ios::binary);
    
        if(!infile){
            cout<<"error"<<endl;
            return;
        }
    
        while(infile.read( (char *)&s, sizeof(s) )){
            int readBytes = infile.gcount();
            cout<<s.name<<" "<<s.age<<endl;
        }
    
        infile.close();
    }

    4.3ファイルの更新
    void updateFile()
    {
        Student s;
        fstream iofile;
        iofile.open("student.dat", ios::in|ios::out|ios::binary);
    
        if(!iofile){
            cout<<"error"<<endl;
            return;
        }
    
        iofile.seekp(2*sizeof(s), ios::beg);
        iofile.write("steven", strlen("steven")+1);
        iofile.seekg(-3*sizeof(s), ios::end);
    
        while(iofile.read( (char *)&s, sizeof(s) )){
            int readBytes = iofile.gcount();
            cout<<s.name<<" "<<s.age<<endl;
        }
    
        iofile.close();
    } 

    4.4ファイルのコピー
    int main(int argc, char* argv[])
    {
        if(argc!= 3){
            cout<<"file name error"<<endl; 
            return 0;
        }
    
        ifstream infile(argv[1], ios::in|ios::binary);
        if(!infile){
            cout<<"error"<<endl;
            return 0;
        }
    
    
        ofstream outfile(argv[2], ios::out|ios::binary);
        if(!outfile){
            cout<<"error"<<endl;
            infile.close(); //            
            return 0;
        } 
    
        char c;
        while(infile.get(c))
            outfile.put(c);
        infile.close();
        outfile.close();
    
        return 0;
    }

    五、まとめ
    ファイルの読み書き、修正、コピーは本質的にほとんど差がなく、基本手順はすべて1)ファイルを開く、2)開くかどうかを判断する、3)ファイルを書く/ファイルを読む、4)ファイルを閉じる.キーコードは次のとおりです.
    ofstream outfile("student.dat", ios::out|ios::binary); ( ifstream infile, ios::in//    
    
    iofile.seekp(2*sizeof(s), ios::beg); ( iofile.seekg) //        
    
    outfile.write((char *)&s, sizeof(s)); ( infile.read) // /   
    
    iofile.close();  //