注:该课程大纲详见 https://www.cnblogs.com/inchbyinch/p/12398921.html
输入输出相关类:
标准流对象:cin, cout, cerr, clog
判断输入流结束:if(cin>>x), if(cin.get(..))等(估计本质上是调用了cin.good(),windows屏幕中输入流结束符为单独一行ctrl+z)
istream类成员函数:
输出输入重定向:
https://blog.csdn.net/bravedence/article/details/77282039
cin读取数据方式常用三种:cin>> , cin.get() , cin.getline() .
cin的条件状态函数:https://www.cnblogs.com/wangduo/p/5940884.html
cin清空输入缓冲区:
文件打开后一定要及时关闭,读取文件之前,要把该文件的写入流关闭,否则出错。
ifstream //文件读取流
ofstream //文件写入流
fstream //文件读写流
//创建流对象时直接关联文件
ofstream fout("clients.dat", ios::out|ios::binary);
//先创建对象,再打开文件
ofstream fout;
fout.open("clients.dat", ios::out|ios::binary);
//判断打开是否成功
if(!fout){
cout << “File open error!”<<endl;
}
//第一个参数:(Windows下)文件的绝对路径和相对路径
"c:\\tmp\\mydir\\some.txt" //绝对路径
"\\tmp\\mydir\\some.txt" //当前盘符的根目录下的tmp\dir\some.txt
"tmp\\mydir\\some.txt" //当前文件夹的tmp子文件夹里面的…
"..\\tmp\\mydir\\some.txt" //当前文件夹的父文件夹下面的tmp子文件夹里面的…
//第二个参数:文件打开方式
ios::in //只读
ios::out //只写
ios::app //追加模式,从文件末尾开始写,防止丢失文件中原来就有的内容
ios::ate //打开一个文件时,将位置移动到文件尾
ios::binary //二进制模式
ios::nocreate //打开一个文件时,如果文件不存在,不创建文件
ios::noreplace // 打开一个文件时,如果文件不存在,创建该文件
ios::trunc // 打开一个文件,然后清空内容
相关函数:
文件指针位置在c++中的用法:
//文件的读写指针
ofstream fout("a1.out", ios::app); //以添加方式打开
long location = fout.tellp(); //取得写指针的位置
location = 10;
fout.seekp(location); // 将写指针移动到第10个字节处
fout.seekp(location, ios::beg); //从头数location
fout.seekp(location, ios::cur); //从当前位置数location
fout.seekp(location, ios::end); //从尾部数location,location可以为负值
ifstream fin(“a1.in”,ios::ate); //打开文件,定位文件指针到文件尾
long location = fin.tellg(); //取得读指针的位置
fin.seekg(10); // 将读指针移动到第10个字节处
因为文件流也是流,所以流的成员函数和流操作算子也同样适用于文件流。
//写一个程序,将文件 in.txt 里面的整数排序后,输出到out.txt
//原in.txt的内容为:1 234 9 45 6 879
//执行后out.txt内容为:1 6 9 45 234 879
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v;
ifstream srcFile("in.txt", ios::in);
ofstream destFile("out.txt", ios::out);
int x;
while(srcFile >> x)
v.push_back(x);
sort(v.begin(),v.end());
for(int i=0; i<v.size(); i++)
destFile << v[i] << " ";
destFile.close(); //文件要及时关闭
srcFile.close();
return 0;
}
//示例1:从键盘输入几个学生的姓名的成绩,并以二进制文件形式保存、读取、修改
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
struct Student {
char name[20];
int score;
};
int main(){
Student s;
//创建写入流文件,写入数据
ofstream outFile( "students.dat", ios::out|ios::binary);
if(!outFile){
cerr << "error" << endl;
return 0;
}
cout << "输入学生信息,格式示例:Jane 68" << endl;
while(cin >> s.name >> s.score)
outFile.write((char*)&s, sizeof(s));
outFile.close(); //需要及时关闭
//创建读取流文件,读取数据
ifstream inFile("students.dat", ios::in|ios::binary );
if(!inFile){
cerr << "error" << endl;
return 0;
}
cout << endl;
while(inFile.read((char* )&s, sizeof(s))){
int readedBytes = inFile.gcount(); //看刚才读了多少字节
cout << s.name << " " << s.score << endl;
}
inFile.close();
//将students.dat文件的第三个学生名字改成Mike
fstream ioFile("students.dat", ios::in|ios::out|ios::binary);
if(!ioFile){
cout << "error";
return 0;
}
ioFile.seekp(2 * sizeof(s), ios::beg); //定位写指针到第三个记录
ioFile.write("Mike", strlen("Mike")+1);
ioFile.seekg(0, ios::beg); //定位读指针到开头,需要显式地说明位置
cout << endl;
while(ioFile.read((char* )&s, sizeof(s)))
cout << s.name << " " << s.score << endl;
ioFile.close();
return 0;
}
//示例2:文件拷贝程序mycopy,将src.dat拷贝到dest.dat
//用法示例:mycopy src.dat dest.dat
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[]){
if(argc != 3){
cout << "File name missing!" << endl;
return 0;
}
ifstream inFile(argv[1], ios::binary|ios::in); //打开文件用于读
if(!inFile ){
cout << "Source file open error." << endl;
return 0;
}
ofstream outFile(argv[2], ios::binary|ios::out); //打开文件用于写
if(!outFile){
cout << "New file open error." << endl;
inFile.close(); //打开的文件一定要关闭
return 0;
}
char c;
while(inFile.get(c)) //每次读取一个字符
outFile.put(c); //每次写入一个字符
outFile.close();
inFile.close();
return 0;
}
不同系统下换行符的表示不同:
导致不同系统下二进制文件和文本文件的区别:
https://www.cnblogs.com/mlgjb/p/7991903.html
缓冲区(buffer)
缓存(cache):
缓冲区和缓存的区别
原文:https://www.cnblogs.com/inchbyinch/p/12398454.html