1.二进制写入,ios::binary
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
struct MyStruct
{
char *p = "北京是帝都";
int num = 20;
double db = 10.98;
char ch = 'a';
};
void main1()
{
ofstream fout("E:\\wen.txt", ios::out);
ifstream fin("E:\\wen.txt");
//下面的大小时24
std::cout << sizeof(MyStruct) << std::endl;
MyStruct my1;
//向文件中写入内容
fout << my1.p << " " << my1.num << " " << my1.db << " " << my1.ch << "\n";
fout.close();
char str[100] = { 0 };
//提取文件内容
fin.getline(str, 100, 0);
std::cout << str << std::endl;
fin.close();
cin.get();
//运行结果是:
//24
//北京是帝都 20 10.98 a
//同时在E盘生成了wen.txt
}
void main()
{
MyStruct my1;
my1.p = "chuheridangwu";
//写入文件,按照二进制的方式写入,要加上:ios::binary
ofstream fout("C:\\bin.bin", ios::binary);
fout.write((char *)&my1, sizeof(my1));
//第一个参数是要写入文件的内存的首地址
//第二个参数是长度
fout.close();
ifstream fin("C:\\bin.bin", ios::binary);
MyStruct newmy1;
fin.read((char *)&newmy1, sizeof(newmy1));
//保存文件读取到内存,内存首地址
//长度
std::cout << newmy1.p << std::endl;
fin.close();
std::cin.get();
//这里的运行结果是:chuheridangwu
}
2.字节的二进制
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
ifstream fin("C:\\write.exe", ios::binary);
ofstream fout("C:\\newwrite.exe", ios::binary);
if (!fin || !fout)
{
std::cout << "文件打开失败";
return;
}
std::cout << "文件拷贝开始\n";
char ch = 0;
while (fout && fin.get(ch))//引用的方式读取到一个字符
{
fout.put(ch);//写入一个字节
}
fin.close();
fout.close();
std::cout << "文件拷贝完成";
std::cin.get();
}
3.二进制内存文件拷贝
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
struct MyStruct
{
int i;
double db;
};
void main()
{
ofstream fout("E:\\big.txt", ios::binary);
MyStruct ss[5] = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 }, { 4, 4.4 }, {5, 5.5 } };
fout.write((char *)ss, sizeof(MyStruct)* 5);
fout.close();
ifstream fin("E:\\big.txt", ios::binary);
MyStruct *p = new MyStruct[5];//动态分配内存
fin.read((char *)p, sizeof(MyStruct)* 5);
fin.close();
for (int i = 0; i < 5; i++)
{
std::cout << p[i].i << " " << p[i].db << std::endl;
}
cin.get();
}
运行结果:
3.随机二进制文件读写
#include<iostream>
#include <fstream>
using namespace std;
void main1a()
{
double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9,10.1 };
ofstream fout("N.txt", ios::binary);
fout.write((char *)db, 80);
fout.close();
double *p = new double[10];
ifstream fin("N.txt", ios::binary);
fin.read((char *)p, 80);
fin.close();
for (int i = 0; i < 10; i++)
{
std::cout << p[i] << endl;
}
std::cin.get();
}
void main2b()//随机位置读取
{
double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9,10.1 };
ofstream fout("N.txt", ios::binary);
fout.write((char *)db, 80);
fout.close();
double *p = new double[5];
ifstream fin("N.txt", ios::binary);
fin.seekg(-40, ios::end);//读写
fin.read((char *)p, 40);
fin.close();
for (int i = 0; i < 5; i++)
{
std::cout << p[i] << endl;
}
std::cin.get();
}
void main()
{
//先读取
double *p = new double[10];
ifstream fin("N.txt", ios::binary);
fin.read((char *)p, 80);
fin.close();
for (int i = 0; i < 10; i++)
{
std::cout << p[i] << endl;
}
//随机写入
double db[] = { 100.9, 200.8, 300.7, 400.6, 500.5 };
//ios::in | ios::out不再清零文件,否则会清零
ofstream fout("N.txt", ios::binary | ios::in | ios::out);
//fout.seekp(40, ios::beg);
fout.seekp(0, ios::end);//写入
fout.write((char *)db, 40);
fout.close();
//再次读取
{
double *p = new double[20];
ifstream fin("N.txt", ios::binary);
fin.read((char *)p, 160);
fin.close();
for (int i = 0; i < 20; i++)
{
std::cout << p[i] << endl;
}
}
std::cin.get();
}
4.随机文件读写
#include<iostream>
#include <fstream>
using namespace std;
void main1()//随机位置读取
{
ofstream fout("p.txt");
fout << "1234567890abcdefghijklmn";
fout.close();
ifstream fin("p.txt");
//fin.seekg(9, ios::beg);//从开始,往前9个字符
fin.seekg(-9, ios::end);//从尾部,倒数9个字符
char ch;
while (fin.get(ch))
{
cout << ch;
}
fin.close();
cin.get();
}
void main2()
{
ofstream fout("px.txt");
fout << "1234567890abcdefghijklmn";
fout.close();
ofstream Fout("px.txt", ios::in | ios::out);
char str[] = "ABCDEFG";
Fout.seekp(0, ios::end);//随机位置进行读写
long size = Fout.tellp();//当前位置距离begin有多少个字节,尾部可以获取文件大小
cout << size << endl;
Fout << str;
Fout.close();
ifstream fin("px.txt");
char ch;
while (fin.get(ch))
{
cout << ch;
}
fin.close();
cin.get();
}