#include<iostream>
using namespace std;
#if 0
#include<sstream>
#include<string>
int main()
{
//类型转换
string res = "100";
int num = 0;
stringstream sst;
sst << res;
sst >> num;
cout << num << endl;
//分隔字符串+类型转换
string ip = "192.188.0.1";
stringstream sst2(ip);
int a1, a2, a3, a4;
char ch;
sst2 >> a1 >> ch >> a2 >> ch >> a3 >> ch >> a4;
cout << a1 << " " << a2 << " " << a3 << " " << a4 << endl;
//拼接字符串+类型转换
stringstream sst3;
num = 3;
char str[] = "14159";
sst3 << num << ch << str;
cout << sst3.str() << endl;
return 0;
}
#endif //字符串流
#if 0
#include<fstream>
struct Student
{
char name[20];
int num;
int age;
char sex;
};
int main()
{
Student stu[3] = { "小6",1001,19,'m',"jack",1002,20,'f',"lucy",1003,22,'f' };
//第一步
//ofstream outfile;
//ofstream outfile("stu.dat",ios::binary)
ofstream outfile("stu.dat", ios::binary);
if (!outfile)
{
cerr << "open error!" << endl;
exit(-1);
//abort();
}
for (int i = 0; i < 3; i++)
{
outfile.write((char*)&stu[i], sizeof(stu[i]));//write 第一个类型必须为const char* (取地址再强转)
}
outfile.close();
//eof end of file
Student rStu[3];
ifstream infile("stu.dat", ios::binary);
if (!infile)
{
cerr << "open error!" << endl;
exit(-1);
//abort();
}
for (int i = 0; i < 3; ++i)
{
infile.read((char*)&rStu[i], sizeof(rStu[i]));
}
infile.close();
for (int i = 0; i < 3; ++i)
{
cout << "name:" << rStu[i].name << endl;
cout << "num:" << rStu[i].num << endl;
cout << "age:" << rStu[i].age << endl;
cout << "sex:" << rStu[i].sex << endl;
}
return 0;
}
#endif//文件流
#if 1
#include<fstream>
struct fileInfo
{
int fileNameSize;//文件名大小
int fileOff;//文件名偏移量
int fileSize;//文件名长度
char fileName[20];//文件名
};
void packFile()
{
fileInfo fileList[4] = { {0,0,0,"1.jpg"},{0,0,0,"2.jpg"},{0,0,0,"3.jpg"},{0,0,0,"4.jpg"} };
fstream file[4];//准备四个文件流对象
int listSize = 0, listNum = 4;//索引大小,文件个数
for (int i = 0; i < 4; ++i)
{
fileList[i].fileNameSize = strlen(fileList[i].fileName + 1);//+\0
listSize += 4 + 4 + 4 + fileList[i].fileNameSize;//获取索引表大小
//计算文件大小
file[i].open(fileList[i].fileName, ios::in | ios::binary);
file[i].seekg(0, ios::end);//第一个偏移值 0往后面偏移 -1往前面偏移 第二个位置 beg cua当前 end最后
fileList[i].fileSize= file[i].tellp();//获取文件大小 指针离开头有多远 先seekg移到最后 再获取指针位置 就是文件大小
file[i].seekg(0, ios::beg);//指针移回文件头
}
fstream newFile("new.pack", ios::out | ios::binary);
newFile.write((char*)&listSize, sizeof(int));
newFile.write((char*)&listNum, 4);
//写索引表
for (int i = 0; i < listNum;++i)
{
//文件偏移量
if (i == 0)
{
fileList[i].fileOff = 8 + listSize;
}
else
{
fileList[i].fileOff = fileList[i - 1].fileOff + fileList[i - 1].fileSize;//后面的图片
}
newFile.write((char*)&fileList[i].fileSize, 4);
newFile.write((char*)&fileList[i].fileOff, 4);
newFile.write((char*)&fileList[i].fileNameSize, 4);
newFile.write((char*)&fileList[i].fileName, fileList[i].fileNameSize);
}
//写文件
char ch;
for (int i = 0; i < listNum; ++i)
{
while (ch=file[i].get(),!file[i].eof())
{
newFile.put(ch);
}
}
for (int i = 0; i < listNum; ++i)
{
file[i].close();
}
newFile.close();
}
//作业
void unpackFile()
{
fileInfo fileList[4];
int listSize, listNum;
//读取索引表
ifstream packFile("new.pack", ios::in | ios::binary);
packFile.read((char*)&listSize, sizeof(int));
packFile.read((char*)&listNum, sizeof(int));
for (int i = 0; i < 4; i++)
{
packFile.read((char*)&fileList[i].fileSize, sizeof(int));
packFile.read((char*)&fileList[i].fileOff, sizeof(int));
packFile.read((char*)&fileList[i].fileNameSize, sizeof(int));
packFile.read(fileList[i].fileName, fileList[i].fileNameSize);
fileList[i].fileName[fileList[i].fileNameSize] = '\0';
}
//提取文件
for (int i = 0; i < listNum; i++)
{
fstream inputFile("new.pack", ios::binary);
inputFile.seekg(fileList[i].fileOff, ios::beg);
ofstream outputFile(fileList[i].fileName, ios::binary);
char ch;
int bytesRemaining = fileList[i].fileSize;
while (bytesRemaining-- && inputFile.get(ch))
{
outputFile.put(ch);
}
inputFile.close();
outputFile.close();
}
packFile.close();
}
int main()
{
unpackFile();
//unpackFile();
return 0;
}
#endif //文件打包