C++输入流

版权声明:版权所有 如有侵权 概不追究 https://blog.csdn.net/weixin_40903417/article/details/88093017

重要的输入流类

istream类最适合用于顺序文本模式输入,cin是其实例

ifstream类支持磁盘文件输入

istringstream

构造输入流对象

如果在构造函数中指定一个文件名,在构造该对象时该文件便自动打开

ifstream myFile("filename");

在调用默认构造函数之后使用open函数来打开文件

ifstream file;

file.open("filename");

打开文件时可以指定模式

ifstream myFile("filename",ios_base::in|ios_base::binary);

使用提取运算符从文本文件输入

输入流相关函数

open函数把该流与一个特定磁盘文件相关联

get函数的功能与提取运算符(>>)很像,主要的不同点是get函数在输入数据时包括空白字符

#include "stdafx.h"
#include <iostream>

using namespace std;


int main()
{
	char ch;
	while ((ch=cin.get())!=EOF)//EOF为文件结束符
	{
		cout.put(ch);
	}
	system("Pause");
	return 0;
}

getline的功能是从输入流中读取多个字符,并且允许指定输入终止字符,读取完成后,从读取的内容中删除终止字符

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int main()
{
	string line;
	cout<<"Type a line terminated by 't':"<<endl;
	getline(cin,line,'t');//第一个参数表示从键盘输入,第二个参数表示输入到line变量,第三个参数可选,表示以't'为分隔符
	cout<<line<<endl;
	system("Pause");
	return 0;
}

read成员函数从一个文件读字节到一个指定的内存区域,由长度参数确定要读的字节数,当遇到文件结束或者在文本模式文件中遇到文件结束标记字符时结束读取

seekg函数用来设置文件输入流中读取数据位置的指针

tellg函数返回当前文件读指针的位置

close函数关闭与一个文件输入流关联的磁盘文件

从文件中读一个二进制记录到一个结构中

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

struct Stu
{
	string name;
	unsigned int age;
};
int main()
{
	Stu a={"Tom",18};
	ofstream file("Student",ios_base::out|ios_base::binary);
	file.write(reinterpret_cast<char*>(&a),sizeof(a));
	file.close();
	ifstream file2("Student",ios_base::in|ios_base::binary);
	if (file2)
	{
		Stu b;
		file2.read(reinterpret_cast<char*>(&b),sizeof(b));
		cout<<setiosflags(ios_base::left);
		cout<<setw(10)<<b.name<<resetiosflags(ios_base::left)<<setw(10)<<b.age<<endl;
	}
	else
	{
		cout<<"The file can not be opened!!!"<<endl;
	}
	file2.close();
	system("Pause");
	return 0;
}

用seekg函数设置位置指针

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
	int a[]={1,2,3,4,5};
	ofstream file("integers",ios_base::out|ios_base::binary);
	file.write(reinterpret_cast<char*>(&a),sizeof(a));
	file.close();
	ifstream file2("integers",ios_base::in|ios_base::binary);
	if (file2)
	{
		file2.seekg(3*sizeof(int));
		int b;
		file2.read(reinterpret_cast<char*>(&b),sizeof(int));
		cout<<b<<endl;
	}
	else
	{
		cout<<"The file can not be opened!!!"<<endl;
	}
	file2.close();
	system("Pause");
	return 0;
}

读一个文件并显示其中元素的位置

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
	ifstream file("integers",ios_base::in|ios_base::binary);
	if (file)
	{
		while (file)
		{
			streampos pos=file.tellg();
			int v;
			file.read(reinterpret_cast<char*>(&v),sizeof(v));
			if(file&&v==1)
				cout<<pos<<endl;
		}
	}
	else
	{
		cout<<"The file can not be opened!!!"<<endl;
	}
	file.close();
	system("Pause");
	return 0;
}

字符串输入流(istringstream)

用于从字符串读取数据

在构造函数中设置要读取的字符串

支持ifstream类的除open、close外的所有操作

典型应用:将字符串转换为数值

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

template<class T>
T fromString(const string &s)
{
	istringstream is(s);
	T v;
	is>>v;
	return v;
}
int main()
{
	int a=fromString<int>("3");
	double b=fromString<double>("3.14");
	cout<<a<<" "<<b<<endl;
	system("Pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40903417/article/details/88093017