C++ 二进制文件

C++ binary file

我使用 ios::binary 创建了一个二进制文件 但我的文件显示的是文本输出。 为什么?以及如何创建以二进制表示形式显示的二进制文件。

#include <iostream>
#include <fstream>
#include<iomanip>
using namespace std;

int main()
{
    ofstream outputFile;
outputFile.open("lab11.bin", ios::binary | ios::out);
int marks,roll_no,n,i;
char name[100];
cout<<"\nEnter the no of students:\n";
cin>>n;

for(i=1;i<=n;i++)
{
    cout<<"\nEnter name of student:\n";
    cin>>name;
    outputFile << name << "\n";
    cout<<"\nEnter Roll no.:\n";
    cin>>roll_no;
    outputFile << roll_no<<"\n";
    cout<<"\nEnter Marks:\n";
    cin>>marks;
    outputFile << marks<<"\n";
}
outputFile.close();

return 0;

}

对于以二进制模式打开的文件,建议使用 writeread 方法。不要使用像 outputFile << roll_no<<"\n";

这样的二进制流输出

C++ references