(创建于2018/1/1)
#include <iostream>
#include<fstream>
using namespace std;
//操作文本文件
void writereadtext() {
//写入文本到一个文件中并创建这个文件,然后读取写入的内容显示到控制台
char* fname = "D://renzhenming.txt";
//创建输入流
ofstream fout(fname);
//创建失败
if (fout.bad()) {
return;
}
//写入
fout << "jack" << endl;
fout << "rose" << endl;
//关闭
fout.close();
//读取
ifstream fin(fname);
if (fin.bad()) {
return;
}
char ch;
while (fin.get(ch)) {
//输出
cout << ch;
}
fin.close();
}
//复制二进制文件(图片)
void copyBinaryFile() {
char *src = "D://beauty.jpg";
char *des = "D://beauty_copy.jpg";
//二进制文件使用binary
ifstream fin(src,ios::binary);
ofstream fout(des, ios::binary);
if (fin.bad() || fout.bad()) {
return;
}
while (!fin.eof()) {
char buff[1024] = {0};
fin.read(buff, 1024);
fout.write(buff, 1024);
}
fin.close();
fout.close();
}
class Person
{
public:
Person()
{
}
Person(char * name, int age)
{
this->name = name;
this->age = age;
}
void print()
{
cout << name << "," << age << endl;
}
private:
char * name;
int age;
};
//对象持久化
void save_object() {
Person p1("renzhenming",27);
Person p2("wuzizi", 26);
//写入
ofstream fout("D://obj.data", ios::binary);
fout.write((char *)(&p1), sizeof(Person));
fout.write((char*)(&p2), sizeof(Person));
fout.close();
//读出
ifstream fin("D://obj.data", ios::binary);
Person temp;
fin.read((char*)(&temp), sizeof(Person));
temp.print();
fin.read((char*)(&temp), sizeof(Person));
temp.print();
}
//标准模板库
#include<string>
void stl() {
string s1 = "haha";
string s2(" sha bi");
string s3 = s1 + s2;
cout << s3 << endl;
//转c字符串
const char * str = s3.c_str();
cout << str << endl;
}
//容器
#include<vector>
void c_list() {
vector<int> v;
v.push_back(12);
v.push_back(11);
v.push_back(10);
for (int i = 0; i < v.size(); i++) {
cout << v[i] << endl;
}
}
void main() {
//writereadtext();
//copyBinaryFile();
//save_object();
//stl();
c_list();
system("pause");
}