写入二进制文件 [英] writing into binary files
问题描述
#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;
}
我不知道该怎么写1个多变量到一个文件中,请大家帮忙,我包括一个例子),也许有更好的方法来写一个文件,请帮助我,这给我很难解决的。
I don't know how to write more than 1 variable into a file, please help, I included an example ;) Maybe there are better ways to write to a file, please help me with this, it's to hard for me to solve.
推荐答案
对于文本的文件,你可以使用类似&LT每行轻松输出一个变量;&LT;
给您的std :: COUT
使用的一个。
For a text file, you could easily output one variable per line using a similar <<
to the one you use with std::cout
.
对于二进制的文件,你需要使用<一个href=\"http://en.cp$p$pference.com/w/cpp/io/basic_ostream/write\"><$c$c>std::ostream::write()$c$c>,其中写一个字节序列。为了您的年龄
属性,你需要 reinter pret_cast
这常量字符*
并要持有 INT
为您的机器架构写入的字节数。请注意,如果你打算读不同的机器上这个二进制日期,就必须采取字大小和字节顺序的考虑。我还建议您零的名称
和姓
缓冲区之前你使用它们,免得你最终未初始化内存的文物在您的二进制文件。
For a binary file, you need to use std::ostream::write()
, which writes a sequence of bytes. For your age
attribute, you'll need to reinterpret_cast
this to const char*
and write as many bytes as is necessary to hold an int
for your machine architecture. Note that if you intend to read this binary date on a different machine, you'll have to take word size and endianness into consideration. I also recommend that you zero the name
and surname
buffers before you use them lest you end up with artefacts of uninitialised memory in your binary file.
此外,没有必要给类的属性传递到 to_file()
方法。
Also, there's no need to pass attributes of the class into the to_file()
method.
#include <cstring>
#include <fstream>
#include <iostream>
class info
{
private:
char name[15];
char surname[15];
int age;
public:
info()
:name()
,surname()
,age(0)
{
memset(name, 0, sizeof name);
memset(surname, 0, sizeof surname);
}
void input()
{
std::cout << "Your name:" << std::endl;
std::cin.getline(name, 15);
std::cout << "Your surname:" << std::endl;
std::cin.getline(surname, 15);
std::cout << "Your age:" << std::endl;
std::cin >> age;
to_file();
}
void to_file()
{
std::ofstream fs("example.bin", std::ios::out | std::ios::binary | std::ios::app);
fs.write(name, sizeof name);
fs.write(surname, sizeof surname);
fs.write(reinterpret_cast<const char*>(&age), sizeof age);
fs.close();
}
};
int main()
{
info ob;
ob.input();
}
一个采样数据文件可能是这样的:
A sample data file may look like this:
% xxd example.bin
0000000: 7573 6572 0000 0000 0000 0000 0000 0031 user...........1
0000010: 3036 3938 3734 0000 0000 0000 0000 2f00 069874......../.
0000020: 0000 ..
这篇关于写入二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!