logo

C++ クラスによるファイル処理

ファイル処理は、データをコンピュータに永続的に保存するために使用されます。ファイル処理を使用すると、データを二次メモリ (ハードディスク) に保存できます。
ファイル処理を実現する方法
ファイル処理を実現するには、次の手順に従う必要があります。
ステップ 1 - ファイルに名前を付ける
STEP 2 - ファイルを開く
STEP 3 - ファイルへのデータの書き込み
STEP 4 - ファイルからデータを読み取る
ステップ 5 - ファイルを閉じます。

C++ でのストリーム :-

実行プログラムに入力を与えると、実行プログラムは出力を返します。実行プログラムへの入力として与えられるバイトのシーケンス、および実行プログラムからの出力として提供されるバイトのシーケンスは、ストリームと呼ばれます。言い換えれば、ストリームはシーケンス内のデータの流れに他なりません。



実行プログラムとキーボードやモニターなどのデバイス間の入出力操作は、コンソール I/O 操作として知られています。実行中のプログラムとファイル間の入出力操作は、ディスク I/O 操作と呼ばれます。

ファイルストリーム操作用のクラス:-

C++ の I/O システムには、ファイル処理メソッドを定義する一連のクラスが含まれています。これらには、ifstream、ofstream、および fstream クラスが含まれます。これらのクラスは、fstream および対応する iostream クラスから派生します。ディスク ファイルを管理するために設計されたこれらのクラスは fstream で宣言されるため、ファイルを使用するプログラムにはこのファイルを含める必要があります。
1.ios:-

  • iosはインプットアウトプットストリームの略です。
  • このクラスは、このクラス階層内の他のクラスの基本クラスです。
  • このクラスには、他のすべての派生クラスが入出力操作に使用する必要な機能が含まれています。

2.iストリーム:-



  • istream は入力ストリームの略です。
  • このクラスはクラス「ios」から派生しています。
  • このクラスは入力ストリームを処理します。
  • このクラスでは抽出演算子 (>>) がオーバーロードされ、ファイルからプログラム実行への入力ストリームを処理します。
  • このクラスは、get()、getline()、read() などの入力関数を宣言します。

3. 反対側:-

  • ostream は出力ストリームの略です。
  • このクラスはクラス「ios」から派生しています。
  • このクラスは出力ストリームを処理します。
  • このクラスでは挿入演算子 (<<) がオーバーロードされ、プログラムの実行からファイルへの出力ストリームを処理します。
  • このクラスは、put() や write() などの出力関数を宣言します。

4.ストリームバッファ:-

シェルスクリプトを実行可能にする
  • このクラスには、入力ストリームと出力ストリームを管理するために使用されるバッファーを指すポインターが含まれています。

5. fstreambase:-



  • このクラスは、ファイル ストリームに共通の操作を提供します。 fstream、ifstream、および ofstream クラスのベースとして機能します。
  • このクラスには、open() 関数と close() 関数が含まれています。

6. ifstream:-

  • このクラスは入力操作を提供します。
  • これには、デフォルトの入力モードを備えた open() 関数が含まれています。
  • get()、getline()、read()、seekg()、および Tellg() 関数を istream から継承します。

7. ストリーム:-

  • このクラスは出力操作を提供します。
  • これには、デフォルトの出力モードを備えた open() 関数が含まれています。
  • ostream から put()、write()、seekp()、および Tellp() 関数を継承します。

8. fストリーム:-

  • このクラスは、同時入出力操作のサポートを提供します。
  • iostream を通じて istream および ostream クラスからすべての関数を継承します。

9. ファイルバッファ:-

  • その目的は、ファイル バッファーを読み取りおよび書き込みできるように設定することです。
  • ファイル バッファ メンバー関数を使用してファイルの長さを決定することもできます。

C++ では主に fstream ヘッダファイルで利用できる fstream、ifstream、ofstream の 3 つのクラスを使ってファイルを扱います。
オフストリーム: ファイルに書き込むストリームクラス
ifstream: ファイルから読み取るストリームクラス
fストリーム: ファイルへの読み取りとファイルへの書き込みの両方を行うストリーム クラス。

次に、読み取りまたは書き込み操作のために特定のファイルを開く最初のステップです。ファイルを開くには次のようにします
1. オブジェクト作成時にコンストラクターにファイル名を渡す
2. オープンメソッドを使用する

たとえば、

コンストラクターを使用してファイルを開く
ifstream (const char* ファイル名、ios_base::openmode モード = ios_base::in);
ifstream fin(filename, openmode) デフォルトでは openmode = ios::in
ifstream fin(ファイル名);

open メソッドを使用してファイルを開く
デフォルトのコンストラクターの呼び出し
ifstream の終了;
fin.open(ファイル名, オープンモード)
fin.open(ファイル名);

モード:

メンバー定数 を意味する アクセス
で * 入力 ファイルを読み取り用にオープン: 内部ストリーム バッファは入力操作をサポートします。
出力 ファイルを書き込み用にオープン: 内部ストリーム バッファは出力操作をサポートします。
バイナリ バイナリ 操作はテキストではなくバイナリ モードで実行されます。
食べた 最後に 出力位置はファイルの末尾から始まります。
アプリ 追加する すべての出力操作はファイルの最後で行われ、既存の内容に追加されます。
トランク 切り詰める ファイルを開く前にファイルに存在していた内容はすべて破棄されます。

デフォルトのオープンモード:

イフストリーム ios::で
川下 ios::out
fストリーム ios::in | ios::out

問題文 : C++ でファイルの読み取りと書き込みを行います。
例:

Input : Welcome in techcodeview.com. Best way to learn things. -1 Output : Welcome in techcodeview.com. Best way to learn things.>

以下を使用して実装します ifstream クラスと ofstream クラス

C++




/* File Handling with C++ using ifstream & ofstream class object*/> /* To write the Content in File*/> /* Then to read the content of file*/> #include> /* fstream header file for ifstream, ofstream,> >fstream classes */> #include> using> namespace> std;> // Driver Code> int> main()> {> >// Creation of ofstream class object> >ofstream fout;> >string line;> >// by default ios::out mode, automatically deletes> >// the content of file. To append the content, open in ios:app> >// fout.open('sample.txt', ios::app)> >fout.open(>'sample.txt'>);> >// Execute a loop If file successfully opened> >while> (fout) {> >// Read a Line from standard input> >getline(cin, line);> >// Press -1 to exit> >if> (line ==>'-1'>)> >break>;> >// Write line in file> >fout << line << endl;> >}> >// Close the File> >fout.close();> >// Creation of ifstream class object to read the file> >ifstream fin;> >// by default open mode = ios::in mode> >fin.open(>'sample.txt'>);> >// Execute a loop until EOF (End of File)> >while> (getline(fin, line)) {> > >// Print line (read from file) in Console> >cout << line << endl;> >}> >// Close the file> >fin.close();> >return> 0;> }>

>

>

時間計算量: O(n)
補助スペース: O(1)

以下を使用して実装します fstream クラス

C++




/* File Handling with C++ using fstream class object */> /* To write the Content in File */> /* Then to read the content of file*/> #include> /* fstream header file for ifstream, ofstream,> >fstream classes */> #include> using> namespace> std;> // Driver Code> int> main()> {> >// Creation of fstream class object> >fstream fio;> >string line;> >// by default openmode = ios::in|ios::out mode> >// Automatically overwrites the content of file, To append> >// the content, open in ios:app> >// fio.open('sample.txt', ios::in|ios::out|ios::app)> >// ios::trunc mode delete all content before open> >fio.open(>'sample.txt'>, ios::trunc | ios::out | ios::in);> >// Execute a loop If file successfully Opened> >while> (fio) {> >// Read a Line from standard input> >getline(cin, line);> >// Press -1 to exit> >if> (line ==>'-1'>)> >break>;> >// Write line in file> >fio << line << endl;> >}> >// Execute a loop until EOF (End of File)> >// point read pointer at beginning of file> >fio.seekg(0, ios::beg);> >while> (fio) {> >// Read a Line from File> >getline(fio, line);> >// Print line in Console> >cout << line << endl;> >}> >// Close the file> >fio.close();> >return> 0;> }>

>

>

時間計算量: O(n)
補助スペース: O(1)

C++




Q: write a single file handling program in c++ to reading and writing data on a file.> #include> #include> > using> namespace> std;> main()> {> >int> rno,fee;> >char> name[50];> > >cout<<>'Enter the Roll Number:'>;> >cin>>rno;>> > >cout<<>' Enter the Name:'>;> >cin>>名前;>> >cout<<>' Enter the Fee:'>;> >cin>>料金;>>' >ofstream fout(>'d:/student.doc'>);> > >fout<' '<' '>rno>>名前>>料金; // ファイルからデータを読み取り Student fin.close(); コート<' '<' '< return 0; }>

>

>

時間計算量: O(1)
補助スペース: O(1)

C++




// Q: WA C++ file handling program to read data from the file called student.doc> #include> #include> using> namespace> std;> main()> {> >int> rno,fee;> >char> name[50];> > >ifstream fin(>'d:/student.doc'>);> > >fin>>rno>>名前>>料金;>>//read data from the file student> > >fin.close();> > >cout<' '<' '< return 0; }>

>

シス
>

時間計算量: O(1)
補助スペース:O(50)