Cプログラミング
 Computer >> コンピューター >  >> プログラミング >> Cプログラミング

C /C++でのバイナリファイルの読み取りと書き込み


書き込み

C ++でバイナリファイルを書き込むには、writeメソッドを使用します。これは、「put」ポインタの位置から開始して、指定されたストリームに指定されたバイト数を書き込むために使用されます。 putポインタが現在ファイルの最後にある場合、ファイルは拡張されます。このポインタがファイルの中央を指している場合、ファイル内の文字は新しいデータで上書きされます。

ファイルへの書き込み中にエラーが発生した場合、ストリームはエラー状態になります。

書き込みメソッドの構文

ostream& write(const char*, int);

読書

C ++でバイナリファイルを読み取るには、readメソッドを使用します。指定されたストリームから指定されたバイト数を抽出し、最初のパラメーターが指すメモリに配置します。ファイルの読み取り中にエラーが発生した場合、ストリームはエラー状態になり、それ以降のすべての読み取り操作は失敗します。

gcount()を使用して、すでに読み取った文字数をカウントできます。次に、clear()を使用して、ストリームを使用可能な状態にリセットできます。

読み取りメソッドの構文

ifstream& write(const char*, int);

アルゴリズム

Begin
   Create a structure Student to declare variables.
   Open binary file to write.
   Check if any error occurs in file opening.
   Initialize the variables with data.
   If file open successfully, write the binary data using write method.
      Close the file for writing.
   Open the binary file to read.
   Check if any error occurs in file opening.
   If file open successfully, read the binary data file using read method.
      Close the file for reading.
   Check if any error occurs.
   Print the data.
End.

サンプルコード

#include<iostream>
#include<fstream>
using namespace std;
struct Student {
   int roll_no;
   string name;
};
int main() {
   ofstream wf("student.dat", ios::out | ios::binary);
   if(!wf) {
      cout << "Cannot open file!" << endl;
      return 1;
   }
   Student wstu[3];
   wstu[0].roll_no = 1;
   wstu[0].name = "Ram";
   wstu[1].roll_no = 2;
   wstu[1].name = "Shyam";
   wstu[2].roll_no = 3;
   wstu[2].name = "Madhu";
   for(int i = 0; i < 3; i++)
      wf.write((char *) &wstu[i], sizeof(Student));
   wf.close();
   if(!wf.good()) {
      cout << "Error occurred at writing time!" << endl;
      return 1;
   }
   ifstream rf("student.dat", ios::out | ios::binary);
   if(!rf) {
      cout << "Cannot open file!" << endl;
      return 1;
   }
   Student rstu[3];
   for(int i = 0; i < 3; i++)
      rf.read((char *) &rstu[i], sizeof(Student));
   rf.close();
   if(!rf.good()) {
      cout << "Error occurred at reading time!" << endl;
      return 1;
   }
   cout<<"Student's Details:"<<endl;
   for(int i=0; i < 3; i++) {
      cout << "Roll No: " << wstu[i].roll_no << endl;
      cout << "Name: " << wstu[i].name << endl;
      cout << endl;
   }
   return 0;
}

出力

Student’s Details:
Roll No: 1
Name: Ram
Roll No: 2
Name: Shyam
Roll No: 3
Name: Madhu

  1. C /C++でのOSに依存しないコードの記述

    実行しているOSに関係なく、オペレーティングシステムと対話できるプログラム。 c / c ++のコンパイラのほとんどは、OSを検出するマクロを定義する能力を持っています。 GCCコンパイラのいくつかのマクロは-です _WIN32:32ビットおよび64ビットのWindowsOS用のマクロ。 _WIN64:64ビットWindowsOS用のマクロ。 _UNIX:UNIXOS用のマクロ。 _APPLE_:macOS用のマクロ。 定義されたこれらのマクロに基づいて、OSに関係なく動作するプログラムを作成しましょう- 例 #include <iostream>

  2. C / C ++のIseek()は、代替n番目のバイトを読み取り、別のファイルに書き込みます

    このチュートリアルでは、代替n番目のバイトを読み取って別のファイルに書き込む方法を理解するためのプログラムについて説明します。 このために、2つの.txtファイルが提供されます。私たちのタスクは、ファイル記述子のポインターを変更するために使用されるIseek()を使用して、あるファイルから別のファイルにコンテンツを書き込むことです。 例 #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h> void func(char arr[], in