64,648
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
struct strange_t
{
string name;
};
int main()
{
strange_t st;
st.name = "ABCDEFG";
cout << st.name << endl;
ofstream ofile;
ofile.open("Temp.bin", ios::binary);
ofile.write( reinterpret_cast<char *> (&st), sizeof(struct strange_t));
ofile.close();
return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
struct strange_t
{
string name;
};
int main()
{
ifstream ifile;
ifile.open("Temp.bin", ios::in | ios::binary);
strange_t st;
ifile.read( reinterpret_cast<char *> (&st), sizeof(strange_t));
while (ifile.good())
{
cout << st.name << endl;
ifile.read( reinterpret_cast<char *> (&st), sizeof(strange_t));
//cout << st->data << endl;
}
ifile.close();
return 0;
}
st.name = "ABCDEFG";
strcpy(st.name, "ABCDEFG");
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream oFile("d:\\test",ios::binary);
string str="Hello";
::operator <<(oFile,str);
//全局<<重载,参数为basic_ostream,basic_string,
//这个方法实现了string写入文件的操作
//特别提示:string 是typedef basic_string<char,std:char_traits<char>,std::allocator<char>> string;
//ofstream也是类似的typedef basic_ofstream<>
str.clear();
oFile.close();
ifstream iFile("d:\\test",ios::binary);
::operator >>(iFile,str);
cout<<str<<endl;
iFile.close();
return 0;
}