#include <iostream>
#include <fstream>
using namespace std;
class info {
private:
char name[15];
char surname[15];
int age;
public:
void input(){
cout<<"Your name:"<<endl;
cin.getline(name,15);
cout<<"Your surname:"<<endl;
cin.getline(surname,15);
cout<<"Your age:"<<endl;
cin>>age;
to_file(name,surname,age);
}
void to_file(char name[15], char surname[15], int age){
fstream File ("example.bin", ios::out | ios::binary | ios::app);
// I doesn't know how to fill all variables(name,surname,age) in 1 variable (memblock)
//example File.write ( memory_block, size );
File.close();
}
};
int main(){
info ob;
ob.input();
return 0;
}
我不知道如何将多个变量写入文件,请帮忙,下面是一个例子;)也许有更好的方法来写入文件,请帮助我解决这个问题,因为对我来说太难了。
ob.input()
,则会发现输入代码中存在错误。尝试在cin>>age
后添加std::cin.ignore(100, '\n');
。 - Robᵩ