[英]reading bytes using fstream c++
我使用fstream對象創建了一個讀/寫字節到二進制文件
#include <iostream>
#include <fstream>
using namespace std;
#define COL_WIDTH 20
int writeBinaryFile(char *desPath);
int readBinaryFile(char *desPath);
int age;
char name[COL_WIDTH];
int recsize = sizeof(int)+sizeof(name);
int n;
int main(){
char desPath[MAX_PATH+1];
char input[2];
while(true){
cout << "Main Menu:\n1 Write Binary File\n2 Read Binary File\n3 EXIT" << endl;
cin.getline(input, 2);
if(atoi(input)== 1){
cout << "Enter destination path:" << endl;
cin.getline(desPath, MAX_PATH+1);
for(;;){
int output = writeBinaryFile(desPath);
if(output == 1) break;
else if(output == 2) { *input = '4'; break;}
}
}
else if(atoi(input) == 2){
cout << "Enter destination path:" << endl;
cin.getline(desPath, MAX_PATH+1);
for(;;){
int output = readBinaryFile(desPath);
if(output == 1) break;
else if(output == 2){ *input = '4'; break;}
}
}
else if(atoi(input) == 3)
break;
else cout << "You entered wrong input, enter only 1 or 2." << endl;
if(atoi(input) == 4) continue;
else break;
}
system("pause");
return 0;
}
int writeBinaryFile(char *desPath){
char option[2];
fstream wBin(desPath, ios::binary | ios::out);
if(!wBin){
cout << desPath << " is not good path."<< endl;
return 1;
}
cout << "Enter name: " << endl;
cin.getline(name,COL_WIDTH);
cout << "Enter age: " << endl;
cin >> age;
cout << "Enter record number: " << endl;
cin >> n;
wBin.seekp(n*recsize);
wBin.write(name, sizeof(name));
wBin.write((char*)&age, sizeof(age));
wBin.close();
cout << "Enter record again? Press:" << endl
<< "Enter to continue" << endl
<< "Q to quit" << endl
<< "M to go back to main menu" << endl;
cin.ignore(256,'\n');
cin.getline(option,2);
if(option[0] == 'q' ||option[0] == 'Q') return 1;
else if(option[0] == 'm' ||option[0] == 'M') return 2;
return 0;
}
int readBinaryFile(char *desPath){
char option[2];
fstream rBin(desPath, ios:: binary | ios:: in);
if(! rBin){
cout << "File not found!" << endl;
return 1;
}
cout << "What record number?" <<endl;
cin >> n;
rBin.seekp((n*recsize));
rBin.read(name,sizeof(name));
rBin.read((char*)&age, sizeof(int));
cout << name <<endl;
cout << age << endl;
rBin.close();
cout << "Print more? Press enter. Press Q to QUIT or M to go back." << endl;
cin.clear();
cin.sync();
cin.getline(option, 2);
if(option[0] == 'q'|| option[0] == 'Q'){rBin.close(); return 1;}
else if(option[0] == 'm' || option[0] == 'M'){rBin.close(); return 2;}
return 0;
}
我首先創建了一個二進制文件,名稱,年齡,記錄編號(字節位置為0)然后在第二個循環上我輸入名稱,年齡,記錄編號2.當我嘗試讀取第一對(char *,int)的字節pos (0)但它返回錯誤的結果但是當我試圖讀取pos(1)中的最后一對時,它返回正確的結果。
我也試過輸入3次,但只有最后一個char *和int是正確的。
為什么在第一個字節(名稱(20字節),年齡(4字節))中得到錯誤的結果但是在最后一個字節中得到正確的結果?
在每次調用writeBinaryFile
,輸出都會重新打開,從而破壞文件的先前內容。
你會想要使用ios::binary | ios::ate | ios::out | ios::in
ios::binary | ios::ate | ios::out | ios::in
ios::binary | ios::ate | ios::out | ios::in
。
但是,如果該文件不存在,則該位掩碼不會創建該文件。 您可以使用此代碼序列解決該問題:
int writeBinaryFile(char *desPath) {
...
// Create the file only if it doesn't exist
fstream(desPath, ios::binary|ios::out|ios::app);
// Now it exists -- open it
fstream wBin(desPath, ios::binary|ios::out|ios::in);
...
}
將@JoeFish歸功於答案的有用部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.