seekg的应用案例

seekg的应用案例在学习C++文件流控制时(链接)我们知道C++有一个标准库fstream该库定义了三个数据类型ofstreamifstream和fstream在练习相应的案例时,seekg()函数掌握的不是很好,后经过多次尝试,可以正常调用了代码如下:#include<fstream>#include<iostream>usingnamespacestd;intmain(){chardata[100];////以写模式打开文件

大家好,又见面了,我是你们的朋友全栈君。

在学习C++文件流控制时(链接)我们知道C++有一个标准库fstream
该库定义了三个数据类型 ofstream ifstream 和 fstream
在练习相应的案例时,seekg() 函数掌握的不是很好,后经过多次尝试,可以正常调用了

代码如下:

#include <fstream>
#include <iostream>
using namespace std;
int main()
{ 

char data[100];
 以写模式打开文件
//ofstream outfile;
//outfile.open("new.out");
//cout << "Writing to the file" << endl;
//cout << "Enter your name: ";
//cin.getline(data, 100);
 向文件写入用户输入的数据
//outfile << data << endl;
//cout << "Enter your name: ";
//cin.getline(data, 100);
 向文件写入用户输入的数据
//outfile << data << endl;
//cout << "Enter your name: ";
//cin.getline(data, 100);
 向文件写入用户输入的数据
//outfile << data << endl;
//cout << "Enter your name: ";
//cin.getline(data, 100);
 向文件写入用户输入的数据
//outfile << data << endl;
//cout << "Enter your name: ";
//cin.getline(data, 100);
 向文件写入用户输入的数据
//outfile << data << endl;
//cout << "Enter your name: ";
//cin.getline(data, 100);
 向文件写入用户输入的数据
//outfile << data << endl;
//cout << "Enter your age: ";
//cin >> data;
//cin.ignore();
 再次向文件写入用户输入的数据
//outfile << data << endl;
 关闭打开的文件
//outfile.close();
char ch;
// 以读模式打开文件
fstream infile;
infile.open("new.out");
cout << "Reading from the file" << endl;
infile >> data;
// 在屏幕上写入数据
cout << data << endl;
cout << infile.tellg() << endl;
// 再次从文件读取数据,并显示它
infile >> data;
cout << data << endl;
cout << "a line" << endl;
cout << infile.tellg() << endl;
infile.get(ch);
cout << ch << endl;
infile.seekg(0L, ios::cur);
cout << infile.tellg() << endl;
infile.get(ch);
cout << ch << endl;
6
infile.seekg(-1L, ios::cur);
cout << infile.tellg() << endl;
infile.get(ch);
cout << ch << endl;
infile.seekg(-1L, ios::cur);
cout << infile.tellg() << endl;
infile.get(ch);
cout << ch << endl;
infile.seekg(-2L, ios::cur);
cout << infile.tellg() << endl;
infile.get(ch);
cout << ch << endl;
//cout << infile.rdbuf() << endl;
cout << "a line" << endl;
//1111
infile.seekg(-3, ios::end);
infile.get(ch);
cout << ch << endl;
//6
infile.seekg(-8, ios::end);
//infile.get(ch);
//cout << ch << endl;
cout << infile.rdbuf() ;
//3
infile.seekg(0, ios::beg);
infile.get(ch);
cout << ch << endl;
cout << 'a line' << endl;
// 关闭打开的文件
infile.close();
return 0;
}

这段代码前半段负责写入程序,后半段从文件中读取数据
需要注意以下几点:

  • 在读取文件时,实例化 fstream 和 ifstream 均可
  • 使用 infile.tellg() 追踪文件指针的位置
  • 使用 cout << infile.rdbuf() ; 输出指针所在处的整个单词
  • ios::cur 在当前指针位置处跳跃
  • ios::beg 从头开始跳跃
  • ios::end 从后往前遍历
  • 使用 ios::end 时,如果想向前遍历,需要输入负的步长
  • infile >> data; 整行输出
    关于 ios::cur 指针部分还是有点迷糊,可以先通过 infile.tellg() 考察指针移动情况,需要用到的时候再深入学习。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/143055.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Unity3D 虚拟现实开发(一)[通俗易懂]

    Unity3D 虚拟现实开发(一)[通俗易懂]大家好,这是我第一篇文章,之前做房地产虚拟现实工作,是时候总结一下制作经验了,现在想将简单的制作流程及设计到的问题整理出来,供大家参考。Unity3D软件安装。以2018.2.14例访问地址:https://unity3d.com/cn/get-unity/download/archive?_ga=2.194947693.1768064749.1541907838-1070007498…

  • JWT单点登录[通俗易懂]

    JWT单点登录[通俗易懂]项目开发视频:SpringCloud微服务开发入门手把手开发基于SpringBoot的员工管理系统亿度云盘~Java小白入门实战超详细的Java知识点汇总单点登录是什么SSO(SingleSignOn)SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。为什么需要单点登录以前分布式系统的多个相关的应用系统,都需要分别进行登录,非常繁琐。原来登录的过程:1)用户输入账号密码2)提交到后台验证,成功后将用户存在Session中3)需要进行登录状态判

  • HttpCanary_django request对象

    HttpCanary_django request对象HttpResponse对象Django服务器接收到客户端发送过来的请求后,会将提交上来的这些数据封装成一个HttpRequest对象传给视图函数。那么视图函数在处理完相关的逻辑后,也需要返回一个响

  • Maven常用指令和配置说明

    Maven常用指令和配置说明本文总结日常工作中经常用到的Maven命令,作为总结,以便学习和回顾。

  • Win7迁移基础知识(2):USMT(用户状态迁移工具)

    Win7迁移基础知识(2):USMT(用户状态迁移工具)

  • java properties native2ascii_使用native2ascii针对中文乱码,进行转码操作,用于native2ascii处理properties文件…

    java properties native2ascii_使用native2ascii针对中文乱码,进行转码操作,用于native2ascii处理properties文件…native2ascii是sunjavasdk提供的一个转码工具,用来将别的文本类文件(比如*.txt,*.ini,*.properties,*.java等等)编码转为Unicode编码。1.如何获取native2ascii.exe?安装了jdk后,假如你是在windows上安装,那么在jdk的安装目录下,会有一个bin目录,该目录下就有我们所需要的native2ascii….

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号