C++ 使用fstream c+读取字节+;
我使用fstream对象将字节读/写到二进制文件中C++ 使用fstream c+读取字节+;,c++,visual-c++,stream,iostream,C++,Visual C++,Stream,Iostream,我使用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)
#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;
}
#包括
#包括
使用名称空间std;
#定义柱宽20
int writeBinaryFile(char*desPath);
int readBinaryFile(char*desPath);
智力年龄;
字符名称[字符宽度];
int recsize=sizeof(int)+sizeof(name);
int n;
int main(){
char desPath[MAX_PATH+1];
字符输入[2];
while(true){
cout在每次调用writeBinaryFile
时,输出都会重新打开,从而破坏文件先前的内容
您需要使用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。你的问题是什么?你的错误和正确结果是什么?我没有立即发现它有任何错误。我必须运行它才能找到问题。你在它运行的过程中仔细检查了它吗?当我创建bin文件时,我输入ff:name:name1,age:1,recordnum:0,然后在第二个循环name2,2,1在第三个循环名3,3,2中,我希望在为recordnum输入0时得到。名称1,1,但我得到的是空的,0…与recordnum 1相同。但是对于recordnum 2,我确实得到了正确的名称3,3…在每次调用writeBinaryFile
时,输出都会重新打开,破坏文件先前的内容。是的,但正如@Rob所说的,因为您正在打开如果仅使用ios::out,先前的文件内容将被销毁。您需要使用ios::binary | ios::ate | ios::out | ios::in