/*
对二进制文件流读写
文本字节:写数字1 实际写入'1'
二进制文件:写数字1 实际写入的是整数1 整数1(4个字节,最低字节是1,高3个字节都是0)
写字符'R'实际输出还是'R'
输出时
例 int age ofsream file;
file.write((char*)&age,sizeof(age));
输入时 ifstream file;
file.read((char*)&age,sizeof(age));
*/
/*文件流的定位
seekg(off_type offset,//偏移量 关于输入
ios::seekdir origin)//起始位置
作用:设置输入流的位置
参数1:偏移量
参数2:相对位置
beg 相对于开始位置
cur 相对于当前位置
end 相对于结束位置
tellg
返回该输入流的位置(距离文件的)
seekp
设置输出流的位置
*/
int main1(void) {
//seekg
string line;
ifstream file;
file.open("源.1.cpp");
if (!file.is_open())return 1;
file.seekg(-50, file.end);
while (!file.eof()) {
getline(file, line);
cout << line << endl;
}
file.close();
//system("pause");
return 0;
}
int main2(void) {
//tellg
ifstream file;
file.open("源.1.cpp");
if (!file.is_open())return 1;
file.seekg(0, file.end);
long long int i=file.tellg();
cout << i;
//system("pause");
return 0;
}
int main5(void) {
//seekp; 12345 4中添加ABC
ofstream file;
file.open("test.txt");
if (!file.is_open())return 1;
file<<"123456789";
file.seekp(4,file.cur);
file << "ABC";
file.close();
//system("pause");
return 0;
}
/*
cin.ignore(count,c);
从输入流中提取并丢弃字符,直到遇到以下三种情况
1.提取的字符达到了参数count指定的数量
2.在输入序列中遇到文件结束eof
3.输入序列中的下一个字符为参数c指定的字符(这个字符会被提取并被丢弃)
count常常取:
std::numeric_limits<std::streamsize>::max()相当于IO流的最大字符个数
常见用法:(把标准缓存区cin的所有数据都清空)
cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
注:不可与 Windows.h头文件一同使用,否则会导致max编译失败
*/