PI is 3.14159
More precisely 3.1415926530e+00
《计算机程序设计》
计算机学院计算机研究所编译系统研究室
~/course
~/course/main.cpp
main.cpp
,随堂编程练习的代码请直接在此文件中编辑操纵子 | 相关标志位/方法 | 描述 |
---|---|---|
dec , oct , hex |
dec , oct , hex |
设置数字进制 |
scientific , fixed , hexfloat , defaultfloat |
scientific , fixed |
设置浮点数输出格式 |
left , right , internal |
left , right , internal |
设置输出对齐方式 |
boolalpha , noboolalpha |
boolalpha |
设置布尔数输入输出格式 |
showbase , noshowbase |
showbase |
显示整数的进制前缀 |
showpoint , noshowpoint |
showpoint |
显示浮点数的小数点 |
showpos , nonshowpos |
showpos |
显示正数符号 |
skipws , noskipws |
skipws |
跳过空白字符 |
uppercase , nouppercase |
uppercase |
使用大写字母表示十六进制前缀 |
setw() |
width() |
设置输出宽度 |
setfill() |
fill() |
设置填充字符 |
setprecision() |
precision() |
设置浮点数输出精度 |
goodbit
:流状态正常eofbit
:流到达文件尾failbit
:输入/输出操作失败(可通过clear()
清除)badbit
:发射能够不可恢复错误(不可通过clear()
清除)方法 | 描述 | 方法 | 描述 |
---|---|---|---|
eof() |
eofbit |
bad() |
badbit |
good() |
goodbit |
fail() |
failbit || badbit |
rdstate() |
返回当前流状态 | clear() |
清除流状态错误 |
思考
cin
读取过的数据,能否再次读取?cout
输出过的数据,能否进行覆盖?f'...'.format(...)
学习目标
学会以文件和字符串为数据载体的输入输出
<fstream>
头文件,并根据需要构造流对象文件流
#include <fstream> // 包含文件流头文件
using namespace std;
int main() {
ifstream fin("input.txt"); // 构造输入文件流对象,从input.txt输入
ofstream fout("output.txt"); // 构造输出文件流对象,向output.txt输出
fstream fio; // 构造双向文件流对象,不关联任何文件
fio.open("file.txt"); // 打开文件file.txt,支持从文件读与向文件写
fin.close(); // 关闭文件流
fout.close(); // 关闭文件流
fio.close(); // 关闭文件流
return 0;
}
明察秋毫
open
方法打开文件),表示建立程序与文件之间的数据通道close
方法),表示关闭此数据通道Picture created by Google Imagen 3
构造文件流对象的同时打开文件
open
方法打开文件明察秋毫
文件名可以是绝对路径或相对路径,若为相对路径,则以当前工作目录为起点
<<
和提取运算符>>
<<
和提取运算符>>
模式 | 描述 |
---|---|
ios::in |
读模式 |
ios::out |
写模式 |
ios::app |
每次写都追加到文件尾部,仅与ios::out 组合使用 |
ios::trunc |
打开文件时清空文件内容,仅与ios::out 组合使用 |
ios::ate |
打开文件后将文件位置置于尾部,与ios::in 与ios::out 均可组合 |
ios::binary |
打开文件为二进制模式,与ios::in 与ios::out 均可组合 |
明察秋毫
打开模式可组合使用,如ios::in | ios::out
表示打开文件用于输入和输出
openmode.cpp
PI is 3.14159
More precisely 3.1415926530e+00
Actually, PI cannot be precisely presented
Try it!
试试,如果不使用ios::app
模式打开文件,运行程序,观察out.txt
的内容
ifstream
,缺省模式为ios::in
ofstream
,缺省模式为ios::out
fstream
,缺省模式为ios::in | ios::out
明察秋毫
ios::in
与ios::out
至少有一个被指定,称为基本模式ios::app
与ios::trunc
仅与ios::out
组合使用有意义ios::ate
指定文件打开后的读写位置为文件末尾(稍后介绍)ios::binary
指定文件以二进制模式打开(稍后介绍)考考你
当试图打开一个不存在的文件,会发生什么?提示:和基本模式相关
nonexistfile.cpp
#include <fstream>
#include <iostream>
#include <iomanip>
int main() {
std::ifstream fin("1.txt", std::ios::in); // 1.txt不存在
std::cout << std::setw(8) << "in " << fin.is_open() << ' ' << fin.fail() << ' ' << fin.bad() << '\n';
std::ofstream fout("2.txt", std::ios::out); // 2.txt不存在
std::cout << std::setw(8) << "out " << fout.is_open() << ' ' << fout.fail() << ' ' << fout.bad() << '\n';
std::fstream fio("3.txt", std::ios::in | std::ios::out); // 3.txt不存在
std::cout << std::setw(8) << "in|out " << fio.is_open() << ' ' << fio.fail() << ' ' << fio.bad() << '\n';
return 0;
}
in 0 1 0
out 1 0 0
in|out 0 1 0
明察秋毫
打开不存在的文件时,若指定了ios::in
,则打开失败,failbit
被置位,否则创建新文件
ios::binary
模式,以二进制格式读写,使用非格式化IO考考你
3.1415923653
时,一共写入了几个字节?3.1415923653
时,一共读取了几个字节?考考你
文本模式与二进制模式下的输入输出,各有什么优缺点?
basic_istream& read(char* s, std::streamsize count); // 从输入流中读取指定数量的字符到缓冲区
basic_ostream& write(const char* s, std::streamsize count); // 从缓冲区输出指定数量的字符到输出流中
unformattedio.cpp
#include <fstream>
using namespace std;
int main() {
fstream fio("data.txt", ios::in | ios::out | ios::binary);
char buf[100];
streamsize count = 100;
fio.read(buf, count); // 读入100个字符到buf
if (fio.fail()) { // 读到文件尾部仍未达到count个字符,fail标志被置位
count = fio.gcount(); // gount返回上一次非格式化读操作实际读取的字节数
fio.clear(); // 清除错误标志
}
fio.write(buf, count); // 将buf中的count个字符写入文件
fio.close();
return 0;
}
考考你
如何将一个双精度数输出到文件中,并且完美保留其精度?
考考你
如何把一个二进制保存的双精度数从文件中读取出来?
read
和write
方法外,流对象还提供了其他非格式化输入方法方法 | 描述 |
---|---|
put() |
向输出流中写入一个字符 |
get() |
从输入流中提取一个字符 |
getline() |
从输入流中提取一行字符串 |
ignore() |
跳过指定数量的字符 |
gcount() |
返回上次输入操作提取的字符数 |
basic_ios::pos_type
),表示从文件头部开始的偏移量ios::ate
模式时置于文件尾,否则置于文件头filepos.cpp
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ofstream fout("data.txt"); // 默认为 ios::out
cout << fout.tellp() << endl; // tellp() 返回输出流的FP,初始化为0
fout << "Hello World!" << endl; // 输出13个字符
cout << fout.tellp() << endl; // 当前位置13
fout.seekp(0, ios::beg); // 将文件指针移动到文件开头
cout << fout.tellp() << endl; // 当前位置0
fout << "Hello World!" << endl; // 输出13个字符
cout << fout.tellp() << endl; // 当前位置13
return 0;
}
0
13
0
13
Hello World!
tellg()
/tellp()
方法查询输入/输出流的FPseekg()
/seekp()
方法设置输入/输出流的FPseekpos.cpp
#include <fstream>
#include <iostream>
int main() {
int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, value = 0;
std::ofstream fout("data.txt", std::ios::binary); // 一次性输出整个数组
fout.write((char *)numbers, sizeof(numbers));
fout.close();
std::ifstream fin("data.txt", std::ios::in | std::ios::binary);
fin.seekg(3 * sizeof(int), std::ios::beg); // 设置文件位置到第3个整数后
fin.read((char *)&value, sizeof(int)); // 读取一个整数
std::cout << value << std::endl; // 输出4
return 0;
}
seekg()
/seekp()
有两种使用方式
考考你
请编写程序,以二进制方式不断从文件读入整数,直到把整个文件读完,输出所有整数
eof.cpp
#include <fstream>
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
ofstream fout("data.txt", ios::binary);
fout.write((char *)arr, sizeof(arr));
fout.close();
ifstream fin("data.txt", ios::binary);
int value;
while (not fin.eof()) {
fin.read((char *)&value, sizeof(value));
cout << value << ' ';
}
return 0;
}
1 2 3 4 5 5
考考你
请编写程序,以二进制方式不断从文件读入整数,直到把整个文件读完,输出所有整数
eof2.cpp
#include <fstream>
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
ofstream fout("data.txt", ios::binary);
fout.write((char *)arr, sizeof(arr));
fout.close();
ifstream fin("data.txt", ios::binary);
int value;
while (fin.read((char *)&value, sizeof(value))) {
cout << value << ' ';
}
return 0;
}
1 2 3 4 5
read
与write
str()
方法--- config: look: handDrawn themeVariables: fontSize: 24px --- mindmap 文件IO与字符串IO 文件IO 文件流 打开与关闭文件 文件打开模式 二进制模式 非格式化IO方法 文件位置 字符串IO 字符串流 str方法
学习目标
计算机程序设计