2015-04-17 208 views
0

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

#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; 

}

回答

2

对于在writeread方法二进制方式使用打开的文件建议。不要使用二进制流输出,如outputFile << roll_no<<"\n";

请参阅C++ references