0
我有一些类似的代码;从文件中添加和检索类的对象 - C++
#include<iostream.h>
#include<string.h>
#include<conio.h>
class MyClass{
char mystring[10];
int mynumber;
public:
MyClass(){}
MyClass(char x[],int y){
strcpy(mystring,x);
mynumber = y;
}
void disp(void){
cout<<mystring<<" - "<<mynumber;
}
void read(void){
cout<<"enter char and number\n";
cin>>mystring>>number;
}
}
int main(){
Myclass test[10];
for(int i=0;i<9;i++){
test[i].read;
//then store the object into file
}
//if user want to display data, then read from file like;
// string1 - 1234
// string2 - 3432
// string9 - 4830
getch();
return 0;
}
我想储存一些字符串和一个相关数字(如电话号码簿)到一个文件,说myfile.txt
为二进制。数据文件可以存储对象MyClass
。如何将信息存储到文件中并从文件中打印整个数据?不用做文件搜索。
使用std :: string而不是C风格的字符串。 –
@ThomasMatthews你能给我一个完整的答案,对不起,这是一个初学者:( –
为什么你想介绍一个二进制文件的所有问题?例如,编译器可能会添加结构成员之间的填充损坏关系。 ,ASCII中的文本与二进制文本相同,占用相同的空间。 –