栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++文件流<fstream>的使用三(输入流的相关函数)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++文件流<fstream>的使用三(输入流的相关函数)

接上篇:(1条消息) C++ 中<fstream>(file stream 文件流)的使用二_一步步走的博客-CSDN博客

1.文件输出流的write()函数:

使用:write(char* buffer ,int count)两个参数 一个是char指针(指向内存数据的起始地址)一个是所写的字节数。

#include
#include
#include
using namespace std;

int main(){
	
	fstream test("test1.dat",ios_base::out|ios_base::binary);//创建文件输出流对象,并以二进制形式打开,即里面是一连串的二进制数据 
	string val[]={"xihg","jsiau","sjia","hello"};
	for(int i=0;i<4;i++){
		test.write((char*)(&val[i]),sizeof(val[i]));//将val[i]中的数据写入文件 
	}
	
	
	return 0;
}

2.文件输出流中的seekp和tellp函数

文件输出流中有一个内部指针指下一个写数据的位置,seekp可以设置这个指针,而tellp可以返回这个该文件位置的指针。

文件输入流

1.输入流的open函数

open函数将一个特定的磁盘文件与输入流对象关联起来。

使用open("filename",open_mode),第一个参数是文件名,第二个参数是打开模式,默认为ios_base::in。打开模式可以用|(与运算符)组合。更多的打开模式见上篇

(2条消息) C++ 中<fstream>(file stream 文件流)的使用二_一步步走的博客-CSDN博客

ifstream in;

in.open("filename",ios_base::in|ios_base::binary);

2.输入流的close函数。

close成员函数关闭于文件输入流相关的磁盘文件。

使用:

ifstream in;
in.open("filename",ios_base::in);
in.close();

3.get()函数

与提取符“>>"很像,区别是">>"提取符不会读入空白字符,而get()会读入空白字符。

示例:新建一个带空格的txt文本文件。

运行如下代码:

#include
#include
using namespace std;

int main(){
	
	ifstream os_in;
	os_in.open("cin_get.txt");
	char c;
	for(int i=0;i<10;i++){
		c=os_in.get();//使用get()成员函数 
	    cout<>c;//使用>>提取符 
	    cout< 

 结果为:

可以看到两者的区别.

4.getline()函数。

getline()有两种形式,一种是成员函数定义在中,一种是非成员函数定义在中。

#include
#include
#include
using namespace std;
int main(){
    char name[20];	
    cin.getline(name,20);//成员函数getline(char* c,streamnumber n),c是承载数据的数组,n是数组大小 
    cout<<"name:"<中 
	cout< 

 结果:

5.输入流的read函数 

read成员函数从一个文件中读取字节到一个指定的储存区域。字节长度由长度参数所决定。

#include
#include
#include
using namespace std;

int main(){	
	
	fstream test("test1.dat",ios_base::out|ios_base::binary);//创建文件输出流对象,并以二进制形式打开,即里面是一连串的二进制数据 
	string val[]={"hei","good","morning","hello"};
	for(int i=0;i<4;i++){
		test.write((char*)(&val[i]),sizeof(val[i]));//将val[i]中的数据写入文件 
	}
	
	ifstream test1("test1.dat",ios_base::in|ios_base::binary);//创建文件输入流对象 
	double value[4];
		for(int i=0;i<4;i++){
		test1.read((char*)(&value[i]),sizeof(val[i]));//将test1.dat中的二进制数据读入程序 
		cout< 

结果

 6.seekg和tellg函数

在文件输入流中,保留一个指向文件下一个要读取数据位置的内部指针,可以用seekg来设置,用tellg来返回。seekg,其中seek是寻找,g是get。seekg(offset,place)。新位将从由 place 给出的起始位置开始,偏移 offset 个字节。offset 形参是一个 long 类型的整数,而 place 可以是 ios 类中定义的 3 个值之一。起始位置可能是文件的开头、文件的当前位置或文件的末尾,这些地方分别由常量 ios::beg、ios::cur 和 ios::end 表示。

#include
#include
using namespace std;

int main(){
	int value[]={3,7,0,5,4};
	ofstream os("intergers.txt",ios_base::out);
	os.write((char*)(&value),sizeof(value));
	os.close();
	
	ifstream is("intergers.txt",ios_base::in);
	if(is){
		is.seekg(3*sizeof(int)); //往后偏移三个int长度的字节,即从指向第一个整数变成指向第四个整数。 
		int v;
		is.read((char*)(&v),sizeof(int));
		cout<<"the 4th intger in the file 'integers' is "< 

结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/864577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号