先上个最简单的DEMO
#include "iostream.h"
#include <fstream>
#include <cstring>
#include <iostream>
using namespace std;
//D:\WorkSpace_C\CaddMore
void showio() {
string PATH = "D:\\WorkSpace_C\\CaddMore\\";
short var = 20000;
ofstream fs;
string txt123_ = PATH + "123.txt";
fs.open(txt123_);
if (fs) {
cout << "fs is complete" << endl;
fs << var;
fs.close();
} else cout << "fs is NOT complete" << endl;
string txt456_ = PATH + "456.txt";
ofstream fs2;
if (fs2) {
fs2.open(txt456_);
if (fs2.is_open() && fs2.good() && !fs2.fail()) {
cout << "is_open:" << fs2.is_open() << " good:" << fs2.good() << " fail:" << fs2.fail()
<< endl;
fs2.write((const char *) &var, sizeof(short));
}
fs2.close();
} else
cout << "fs2 is NOT complete" << endl;
short value = 0;
ifstream fi;
if (fi) {
cout << "fi is complete" << endl;
fi.open(txt456_);
fi.read((char *) &value, sizeof(short));
cout << "value = " << value << endl;
} else cout << "fi is NOT complete" << endl;
}
输出结果
fs is complete
is_open:1 good:1 fail:0
fi is complete
注意:
1.流的默认打开方式
fstream(输入输出文件流):支持文件的输入与输出操作;
ifstream(输入文件流):支持从文件中输入操作;
ofstream(输出文件流):支持向文件写入的操作;
ofstream fs1("123.txt", ios::out); 默认写方式打开输出流
ifstream fs2("123.txt", ios::in);默认输入方式打开输入流
fstream fs3("123.txt", ios::in|ios::out);默认输出或输入方式打开输入输出流
2.第二个参数说明
ios::in
为输入(读)而打开文件
ios::out
为输出(写)而打开文件
ios::ate
初始位置:文件尾
ios::app
所有输出附加在文件末尾
ios::trunc
如果文件已存在则先删除该文件
ios::binary
二进制方式
这些方式是能够进行组合使用的,以“或”运算(“|”)的方式
- fs2.is_open() , fs2.good(),!fs2.fail()这三个方法来判断流是否正常开启。
对象 | 含义 | 对应设备 | 对应的类 | c语言中相应的标准文件 |
cin | 标准输入流 | 键盘 | istream_withassign | stdin |
cout | 标准输出流 | 屏幕 | ostream_withassign | stdout |
cerr | 标准错误流 | 屏幕 | ostream_withassign | stderr |
clog | 标准错误流 | 屏幕 | ostream_withassign | stderr |
标准输入流 cin
cin.get() //读入一个字符并返回它的值
cin.get(一个参数) //读入一个字符并把它存储在ch
cin.get(两个参数) //可以读取字符串
cin.get(三个参数) //可以读字符串
cin.getline()
cin.ignore() //读取字符并忽略指定字符
cin.peek() //检查下一个输入的字符,不会把字符从流中移除
cin.putback() //返回一个字符给一个流
实例:
string txt123_ = PATH + "123.txt";
ifstream fs_in;
fs_in.open(txt123_, ios::in);
if (!fs_in) return;
cout << "=========从流中读取一个字符========" << endl;
char cha = fs_in.get();
cout << "cha = " << cha << endl;
cout << "=========放回去:putback(cha):" << cha << endl;
fs_in.putback(cha);
cout << "=========判断流中的第一个字符"<< cha <<"是不是放进去的那个字符=========" << endl;
int look = fs_in.peek();
if (look == (int) cha) {
cout << "cin.peek()放进去的字符在第一个位置" << endl;
} else {
cout << "cin.peek()放进去的字符不是在第一个位置" << endl;
}
cout << "=========从流中读取2个字符========" << endl;
char some[2];
fs_in.get(some, 3);
cout << "some = " << some << endl;
cout << "=========从流中忽略5个字符,再读取10个字符=========" << endl;
char someTen[10];
fs_in.ignore(5);
fs_in.get(someTen, 11, EOF);
cout << "someTen = " << someTen << endl;
cout << "=========读取一行========" << endl;
char buff[1024] = {0};
fs_in.getline(buff, 1024); //通过getline函数读取字符串
cout << "buff = " << buff << endl;
fs_in.close();
system("pause");
标准输出流 cout
实例:
short var = 20000;
ofstream fs;
cout << "=========打开方式1========" << endl;
string txt123_ = PATH + "456.txt";
fs.open(txt123_);
if (!fs) {
cout << "打开文件失败" << txt123_ << endl;
cout << "=========打开方式2========" << endl;
fs = ofstream(txt123_, ios::out);
}
if (!fs) return;
cout << "用 << 输出" << endl;
fs << var << endl;
fs << "真的是你吗?" << endl;
//put 一次一个字符
cout << "=======put 一次一个字符=======" << endl;
fs.put('Y');
cout << "=======write 支持文本类型和二进制类型:指定输出字符=======" << endl;
fs.write((char *) &var, sizeof(var));
fs.close();
文件流 fstream
实例
//文件流对象写文件
string fname = PATH + "456.txt";
char buff[1024];
//构造函数初始化 也叫拷贝函数
fstream fileS(fname, ios::in | ios::out);
fileS << "abdfd\n";
fileS << "11111\n";
//刷新
fileS.flush();
fileS.close();
//文件流对象读文件内容
fstream fileR(fname, ios::in | ios::out);
//通过右移运算符读出字符串
fileR >> buff;
cout << buff << endl;
fileR.close();
对象流
注意这里用的结构体 效果和class没区别
enum Sex {
man = 1,
wuman = 0
};
struct Student {
string name;
int num;
Sex sex;
int age;
string &getName() {
return name;
}
Student() {}
Student(const string &name, int num, Sex sex, int age) : name(name), num(num), sex(sex),
age(age) {}
Student(string &name)
: name(name), num(num = 1000 * rand()), sex(sex = man), age(age = 18) {
}
Sex getSex() const {
return sex;
}
void toString() {
cout << " name = " << name << " num = " << num << " sex = " << sex << " age = " << age << endl;
}
};
实现:比较简单 感觉和java很像了
Student maqi(*new string("马齐"));
Student zhangsan(*new string("张三"));
Student lisi(*new string("李四"));
Student wanger(*new string("王二麻子"));
string fname = PATH + "456.txt";
fstream fs(fname, ios::binary | ios::out);
if (!fs) {
cout << "文件打开失败" << endl;
}
fs.write((char *) &maqi, sizeof(Student));
fs.write((char *) &zhangsan, sizeof(Student));
fs.write((char *) &wanger, sizeof(Student));
fs.write((char *) &wanger, sizeof(Student));
fs.flush();
fs.close();
fstream fsR(fname, ios::binary | ios::in);
if (!fsR) {
cout << "文件打开失败" << endl;
}
Student maqiR;
fsR.read((char *) &maqiR, sizeof(Student));
maqiR.toString();
Student zhangsaniR;
fsR.read((char *) &zhangsaniR, sizeof(Student));
zhangsaniR.toString();
Student lisiiR;
fsR.read((char *) &lisiiR, sizeof(Student));
lisiiR.toString();
Student wangerR;
fsR.read((char *) &wangerR, sizeof(Student));
wangerR.toString();
//优化
while (!fsR.eof()) {
Student maqiR;
fsR.read((char *) &maqiR, sizeof(Student));
maqiR.toString();
}
fsR.flush();
fsR.close();