2015-04-17 134 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