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中的文本與二進制文本相同,佔用相同的空間。 –