Part11 流类库与输入输出 11.3输入流
1输入流概述
重要的输入流类
istream类最适合用于顺序文本模式输入。cin是其实例。
ifstream类支持磁盘文件输入。
istringstream
构造输入流对象
如果在构造函数中指定一个文件名,在构造该对象时该文件便自动打开。
ifstream myFile("filename");
在调用默认构造函数之后使用open函数来打开文件。
ifstream myFile; //建立一个文件流对象
myFile.open("filename"); //打开文件"filename”
打开文件时可以指定模式
ifstream myFile("filename", iosbase::in | iosbase::binary);
使用提取运算符从文本文件输入
提取运算符(>>)对于所有标准C++数据类型都是预先设计好的。
是从一个输入流对象获取字节最容易的方法。
ios类中的很多操纵符都可以应用于输入流。但是只有少数几个对输入流对象具有实际影响,其中最重要的是进制操纵符dec、oct和hex。
输入流相关函数
open 把该流与一个特定磁盘文件相关联。
get 功能与提取运算符(>>)很相像,主要的不同点是get函数在读入数据时包括空白字符。
getline 功能是从输入流中读取多个字符,并且允许指定输入终止字符,读取完成后,从读取的内容中删除终止字符。
read 从一个文件读字节到一个指定的内存区域,由长度参数确定要读的字节数。当遇到文件结束或者在文本模式文件中遇到文件结束标记字符时结束读取。
seekg 用来设置文件输入流中读取数据位置的指针。
tellg 返回当前文件读指针的位置。
close 关闭与一个文件输入流关联的磁盘文件。
2输入流举例应用
//例11-7 get函数应用举例 #include <iostream> using namespace std; int main() { char ch; while ((ch = cin.get()) != EOF) cout.put(ch); return 0; } //例11-8为输入流指定一个终止字符 #include <iostream> #include <string> using namespace std; int main() { string line; cout << "Type a line terminated by 't' " << endl; getline(cin, line, 't'); cout << line << endl; return 0; } //例11-9 从文件读一个二进制记录到一个结构中 #include<iostream> #include<fstream> #include<cstring> using namespace std; struct SalaryInfo{ unsigned id; double salary; }; int main(){ SalaryInfo employee1 = {600001, 8000}; ofstream os("payroll", ios_base::out | ios_base::binary); os.write(reinterpret_cast<char *>(&employee1), sizeof(employee1)); os.close(); ifstream is ("payroll", ios_base::in | ios_base::binary); if(is){ SalaryInfo employee2; is.read(reinterpret_cast<char *>(&employee2), sizeof(employee2)); cout << employee2.id << " " << employee2.salary << endl; }else{ cout << "ERROR: Cannont open file 'payroll'." <<endl; } is.close(); return 0; } //例11-10用seekg函数设置位置指针 #include<iostream> #include<fstream> #include<cstring> using namespace std; int main(){ int values[] = {3,7,0,5,4}; ofstream os("integers", ios_base::out | ios_base::binary); os.write(reinterpret_cast<char *>(values), sizeof(values)); os.close(); ifstream is("integers", ios_base::in | ios_base::binary); if(is){ is.seekg(3 * sizeof(int)); int v; is.read(reinterpret_cast<char *>(&v), sizeof(int)); cout << "The 4th integer in the file 'integers' is " << v << endl; }else{ cout <<"ERROR: Cannot open file 'integers'." << endl; } return 0; } //例11-11 读一个文件并显示出其中0元素的位置 #include<iostream> #include<fstream> #include<cstring> using namespace std; int main(){ ifstream file("integers", ios_base::in | ios_base::binary); if(file){ while(file){ //读到文件尾file为0 streampos here = file.tellg(); int v; file.read(reinterpret_cast<char *>(&v), sizeof(int)); if(file && v == 0) cout << "Position " << here << " is 0" << endl; } }else{ cout << "ERROR: Cannot open file 'integers'." << endl; } file.close(); return 0; }
3从字符串输入
字符串输入流( istringstream)
用于从字符串读取数据
在构造函数中设置要读取的字符串
功能
支持ifstream类的除open、close外的所有操作
典型应用
将字符串转换为数值
//例11-12用istringstream将字符串转换为数值 #include<iostream> #include<sstream> using namespace std; template<class T> inline T fromString(const string &str){ istringstream is(str); T v; is >> v; //从字符串输入流中读取变量v return v; //返回变量v } int main(){ int v1 = fromString<int>("5"); cout << v1 << endl; double v2 = fromString<double>("1.2"); cout << v2 << endl; return 0; }