动漫网站怎么做,网站建设列表横向,网站制作中企动力公司,源码站系列文章目录
C IO库 文章目录 系列文章目录前言一、文件IO概述coutcin其他istream类方法 文件输入和输出内核格式化总结 前言 一、文件IO
概述
c程序把输入和输出看作字节流。输入时#xff0c;程序从输入流中抽取字节#xff1a;输出时#xff0c;程序将字节流插入到输…系列文章目录
C IO库 文章目录 系列文章目录前言一、文件IO概述coutcin其他istream类方法 文件输入和输出内核格式化总结 前言 一、文件IO
概述
c程序把输入和输出看作字节流。输入时程序从输入流中抽取字节输出时程序将字节流插入到输出流中。 一般输入和输出都有缓冲区。
C程序通常在用户按下回车键时刷新输入缓冲区。
cin: 标准输入流 cout: 标准输出流 如果输出被重定向到文件则标准错误流依然会被输出到屏幕 cerr: 标准错误流无缓冲区 clog: 标准错误流有缓冲区
cout
ostream类将输出转化为字符字节流
put() 显示字符 cout.put(‘W’).put(65.1); write() 显示字符串 cout.write(“abcd”, 2).write(“ab”, 9); long val 234234; cout.write((char*)val, sizeof(long)); 将val内存中的数据作为字节字符输出到屏幕
输出缓冲区 输出缓冲区为512字节或为其整数倍 当输出为磁盘时有缓冲区当输出为屏幕时会进行优化也可显示指出刷新输出缓冲区 cout flush (刷新输出换出区) endl(刷新输出换出区并插入换行符); flush(cout)也可
格式化 ostream插入运算符将输入转换成文本输出。
数字显示方式dec, hex, oct调整字段宽度cout.width(x)填充字符cout.fill(‘*’)设置浮点数的显示精度cout.precision(2)打印末尾的0和小数点cout.setf(ios_base::uppercase) 常量含义ios_base::boolalpha输入和输出bool值可以为true或falseios_base::showbase对于输出使用C基数前缀(0, 0x)ios_base::showpoint显示末尾的小数点ios_base::uppercase对于16进制输出使用大写字母E表示法ios_base::showpos在正数前加 标准控制符略头文件iomanip
cin
cin 对象将标准输入表示为字节流。
对于如下代码 int ele; cin ele; 假设键入下面的字符 -123Z 运算符将读取字符-123因为它们都是整数的有效部分。但Z字符不是有效字符因此输入中最后一个可接受的字符是3。Z将留在输入流中下一个cin语句将从这里开始读取。与此同时运算符将字符序列-123转换为一个整数值并将它付给ele。 输入有时可能没有满足程序的期望。例如假设输入的是Zcar而不是-123Z。在这种情况下抽取运算符将不会修改ele的值并返回0如果istream对象的错误状态被设置if或while语句将判定该对象为false。返回值false让程序能够检查输入是否满足要求。
while(cin num);由于输入被缓冲。因此通过键盘输入的第二行在用户按下enter之前不会被发送给程序。然而循环在字符Z处停止了对输入的处理因此它不与任何一种浮点格式匹配。输入与预期格式不匹配反过来将当导致表达式cininput的结果为false因此while循环被终止。
成员描述eofbit如果到达文件尾则设置为1badbit如果流被破坏则设置为1例如文件读取错误failbit如果输入操作未能读取预期的字符或输出操作没有写入预期的字符则设置为1goodbit另一种表示0的方法good()如果流可以使用所有的位都被清除则返回trueeof()如果eofbit被设置则返回truebad()如果badbit被设置则返回truefail()如果badbit或failbit被设置则返回truerdstate()返回流状态exceptions()返回一个位掩码指出那些标记导致异常被引发exceptions(isostate ex)设置那些状态将导致clear()引发异常例如如果ex是eofbit则如果eofbit被设置clear()将引发异常clear(iostate s)将流状态设置为ss的默认值为0(goodbit)如果(restate() exceptions())!0, 则引发异常 basic_ios::failuresetstate(iostate s)调用clear(rdstate() | s)。这将设置与s中设置的为对应的流状态为其他流状态为保持不变
其他istream类方法
int ct 0;
char ch;
do{cin.get(ch);cout ch;ct;
}while(ch ! \n\);
cout ct endl;
// 若为下面的则跳过空格
int ct 0;
char ch;
do{cin ch;cout ch;ct;
}while(ch ! \n\);
cout ct endl;文件输入和输出
#include fstreamofstream fout(file.name); // or f.open(xxx);
ifstream fin(file.name);char ch;
while(fin.get(ch)); // 一次读取一个字符直到文件结尾EOF以默认模式打开文件进行输出将自动把文件的长度阶短为零这相当于删除已有的内容
流状态
if(fin); // 打开成功为1失败为0
if(fin.is_open()); // 可检查“文件模式”是否正确// 一次打开多个文件
ifstream fin;
fin.open(xxx1);
...
fin.close();
fin.clear();
fin.open(xxx2);
...
fin.close();文件模式
常量含义io_base::in打开文件以便读取io_base::out打开文件以便写入io_base::ate打开文件并移到文件尾ios_base::app追加到文件尾ios_base::trunc如果文件存在则截短文件ios_base::binary二进制文件
ofstream.open(xxx, ios_base::out | ios_base::trunc);
ofstream.open(xxx, ios_base::out | ios_base::app);文本格式二进制文件
以二进制形式写读文件
ofstream fout(planets.dat, ios_base::out | ios_base::app | ios_base::binary);
fout.write( (char*)pl, sizeof(pl));ifstream fin(planets.dat, ios_base::in | ios_base::binary);
fin.read((char*)pl, sizeof(pl);随机存取 直接移动不是依次移动到文件的任何位置。随机存取常被用于数据库文件程序维护一个独立的索引文件该文件指出数据在主数据文件中的位置。
fstream类从iostream类派生后者基于istream和ostream连个类因此它继承了它们的方法。它还继承了两个缓冲区一个用于输入一个用于输出并能同步化这两个缓冲区的处理。也就是说当程序读写文件时它将协调地移动输入缓冲区中的输入指针和输出缓冲区中的输出指针。 读写模式 finout.open(file, ios_base::in | ios_base::out | ios_base::binary); seekp ofstream seekg ifstream seekp()用于将输出指针移到指定的文件位置。seekg()原型 basic_istreamcharT, traits seekg(off_type, ios_base::seekdir); basic_istreamcharT, traits seekg(pos_type); equal to: istream seekg(streamoff, ios_base::seekdir); istream seekg(streampos); 第一个原型定位到离第二个参数指定的文件位置特定距离的位置第二个原型定位到离文件开头特定距离的位置。 assume fin is a ifstream object:
fin.seekg(30, ios_base::beg); // 30 bytes beyond the beginning
fin.seekg(-1, ios_base::cur); // back up one byte
fin.seekg(0, ios_base::end); // go to the end of the file将streampos位置看作相对文件开始处的位置(第一个字节编号为0)偏移量 fin.seekg(112); 如果要检查文件指针的当前位置则对于输入流可以使用tellg()方法对于输出流可以使用tellp()方法它们返回streampos。fstream对象的输入输出指针同步。如果使用istream ostream对象则输入输出指针不同步。
打开文件、移到文件开头并显示文件内容
fstream finout; // read and write streams
finout.open(file, ios::in | ios::out | ios::binary);
// note: some unix systems require omitting | ios::binary
int ct 0;
if (finout.is_open()) {finout.seekg(0); // go to beginningcout Here file file:\n;while(finout.read((char*)pl, sizeof(pl)){cout ct : setw(LIM) pl.name : setprecision(0) setw(12) pl.population setprecision(2) setw(6) pl.g endl;}if (finout.eof()) { // 读取到文件尾finout.clear(); // clear eof flag} else { // 没有读到文件尾而是其他故障导致cerr Error in reading file .\n;exit(EXIT_FAILURE);}
} else { // 若文件打开失败cerr file could not be opened.\n;exit(EXIT_FAILURE);
}cout Enter the record number you wish to change: ;
long rec;
cin rec;
eatline(); // get rid of newline
if (rec 0 || rec ct) {cerr Invalid record number.\n;exit(EXIT_FAILURE);
}
streampos place rec * sizeof pl; // convert to streampos type
finout.seekg(place); // random access
finout.read((char*)pl, sizeof pl);
cout Your selec:\n;
cout rec : setw(LIM) pl.name : setprecision(0) setw(12) pl.population setprecision(2) setw(6) pl.g endl;
if (finout.eof()) finout.clear();
cout Enter planet name: ;
cin.get(pl.name, LIM);
eatline();
cout Enter population: ;
cin pl.population;
cout Enter planets acceleration of gravity: ;
cin pl.g;
finout.seekp(place);
finout.write((char*)pl, sizeof pl) flush; // 刷新输出if (finout.fail()){cerr Error on attempted write\n;exit(EXIT_FAILURE);
}stdio stddef stdlib unistd
使用临时文件 #include char* tmpnam(char* pszName); 生成临时文件名 tmpnam()函数创建一个临时文件名将它放在pszName指向的C-风格字符串中。常量L_tmpnam和TMP_MAX (二者都是在cstdio中定义的)限制了文件名包含的字符数以及在确保当前目录中不生成重复文件名的情况下可被调用的最多次数
内核格式化 iostream族支持程序与中断之间的I/O而fstream族使用相同的接口提供程序和文件之间的I/O。C库还提供了sstream族它们使用相同的接口提供程序和string对象之间的I/O。也就是说可以使用于cout 的ostream方法将格式化信息写入到string对象中并使用istream方法(如getline())来读取string对象中的信息。读取string对象中格式化信息或将格式化信息写入到string对象中被称为内核格式化(incore formatting)。
#include iostream
#include sstream
#include stringint main()
{using namespace std;ostringstream outstr; // manages a string streamstring hdisk;cout Whats the name of your hard disk? ;getline(cin, hdisk); // getlineint cap;cout Whats its capacity in GB? ;cin cap;// write formatted information to string streamoutstr The hard disk hdisk has a capacity of cap gigabytes.\n;string result outstr.str(); // save resultcout result; // show contents
}...
string lit it was a dark and storm day;
istringstream instr(lit); // use buf for input
string word;
while(instr word)cout word endl;总结
get() 单字符输入 getline() 字符串输入
seekg()和seekp()函数提供对文件的随机存取。这些类方法使得能够将文件指针放置到相对于文件开头、文件尾和当前位置的某个位置。tellg()和tellp()方法报告当前的文件位置。
获取文件大小
ifstream fin(planets.dat, ios::in | ios::binary);
fin.seekg(0, ios::end);
unsigned long long pos fin.tellg();
cout pos endl;random.cpp #include iostream
#include fstream
#include iomanip // 格式化输出
#include cstdlib // for exitusing namespace std;const int LIM 20;struct planet
{char name[LIM];double population;double g;
};const char* file planets.dat;
inline void eatline()
{while (cin.get() ! \n);
}int main()
{planet pl;cout fixed;fstream finout;finout.open(file, ios::in | ios::out | ios::binary);int cnt 0;if (finout.is_open()) {finout.seekg(0); // go to beginningcout Here, contents file file:\n;while (finout.read((char*)pl, sizeof pl)) {cout cnt : setw(LIM) pl.name : setprecision(0) setw(12) pl.population setprecision(2) setw(6) pl.g endl;}if (finout.eof()) {finout.clear();} else {cerr Error in reading file .\n;exit(EXIT_FAILURE);}} else {cerr file could not be opened.\n;exit(EXIT_FAILURE);}// change a recordcout Enter the record number you wish to change: ;long rec;cin rec;eatline();if (rec 0 || rec cnt) {cerr Invalid record number.\n;exit(EXIT_FAILURE);}streampos place rec * sizeof pl;finout.seekg(place);if (finout.fail()) {cerr Error on attempted seek\n;exit(EXIT_FAILURE);}finout.read((char*)pl, sizeof pl);cout Your selection:\n;cout rec : setw(LIM) pl.name : setprecision(0) setw(12) pl.population setprecision(2) setw(6) pl.g endl;if (finout.eof()) {finout.clear();}cout Enter name: ;cin.get(pl.name, LIM);eatline();cout Enter popu: ;cin pl.population;cout Enter g: ;cin pl.g;finout.seekp(place);finout.write((char*)pl, sizeof pl) flush;if (finout.fail()) {cerr Error\n;exit(EXIT_FAILURE);}// show revised filecnt 0;finout.seekg(0); // goto beginning of filecout Here file file:\n;while (finout.read((char*)pl, sizeof pl)) {cout cnt : setw(LIM) pl.name : setprecision(0) setw(12) pl.population setprecision(2) setw(6) pl.g endl;}finout.close();cout Done.\n;
}writedat.cpp
#include iostream
#include fstream
#include iomanip // 格式化输出
#include cstdlib // for exitconst int LIM 20;struct planet
{char name[LIM];double population;double g;
};int main()
{using namespace std;ofstream fout(planets.dat, ios::out | ios::binary);if (!fout) {return 0;}planet p1 {earth, 1000, 9.80001};planet p2 {mars, 34234, 4.555};planet p3 {moon, 45, 3.55};fout.seekp(0);fout.write((char*)p1, sizeof p1);fout.write((char*)p2, sizeof p2);fout.write((char*)p3, sizeof p3);fout.close();}