【发布时间】:2011-12-21 12:31:36
【问题描述】:
我使用 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。 当我尝试读取字节 pos(0) 的第一对(char*, int) 但它返回错误的结果但是当我尝试读取 pos(1) 中的最后一对时它返回正确的结果。
我也尝试了 3 次输入,但只有最后一个 char* 和 int 是正确的。
为什么在第一个字节(名称(20bytes),年龄(4bytes))中得到错误的结果,但在最后一个字节中得到正确的结果?
【问题讨论】:
-
您的问题是什么?你的错误和正确结果是什么?
-
我没有立即发现它有什么问题。我必须运行它才能找到问题。你有没有在它运行时穿过它?
-
当我创建 bin 文件时,我输入 ff: name:name1, age:1, recordnum:0 然后在第二个循环 name2,2,1 然后在第三个循环 name3,3,2...当我为recordnum ..name1, 1输入0时,我希望得到,但我只是空白,0 ...与recordnum 1相同..但是对于recordnum 2,我确实得到了正确的name3, 3 ...跨度>
-
在每次调用
writeBinaryFile时,都会重新打开输出,破坏文件的先前内容。 -
是的,但正如@Rob 所说,由于您仅使用 ios::out 打开它,因此以前的文件内容将被破坏。你会想要使用 ios::binary | ios::吃 | ios::out | ios::in
标签: c++ visual-c++ stream iostream