关于seekg失效的问题

摘要:
当file.eof()=1的时候seekg就不好用了,当file.eof()=0的时候seekg是好用的。也就是说当一个文件读到尾部以后,不能再用seekg来移动或者定位了。通过建立该文件新的对象能解决这个问题。如果只是输出的话可以用streambuf的rdbuf#include#include#includeusingnamespacestd;intmain(){ofstreamofile;ofile

当file.eof()=1的时候seekg就不好用了,
当file.eof()=0的时候seekg是好用的。
也就是说当一个文件读到尾部以后,
不能再用seekg来移动或者定位了。
通过建立该文件新的对象能解决这个问题。

如果只是输出的话可以用streambuf的rdbuf

#include<fstream>#include<iostream>#include<string>
using namespacestd;

intmain(){
    ofstream ofile("test.txt");
    ofile<<"hello this is testing fstream!";
    ofile<<endl;
    ofile.close();

    ifstream ifile("test.txt");
    stringline;
    for(int i=0; i<3; i++){
        cout<<"this is "<<i<<"file"<<endl;
        ifile.clear();
        while(getline(ifile,line)){
            cout<<line<<endl;
        }
        cout<<"eof: "<<ifile.eof()<<endl;
        ifile.seekg(0,ios::beg);
    }
    ifile.close();
}

输出:

this is 0 file
hello this is testing fstream!
eof: 1
this is 1 file
eof: 1
this is 2 file
eof: 1

可以改用rdbuf

关于seekg失效的问题第1张关于seekg失效的问题第2张View Code
1 #include<fstream>
2 #include<iostream>
3 #include<string>
4 using namespacestd;
5 
6 intmain(){
7     ofstream ofile("test.txt");
8     ofile<<"hello this is testing fstream!";
9     ofile<<endl;
10 ofile.close();
11 
12     ifstream ifile("test.txt");
13     stringline;
14     for(int i=0; i<3; i++){
15         cout<<"this is "<<i<<"file"<<endl;
16         cout<<ifile.rdbuf();
17         cout<<"eof: "<<ifile.eof()<<endl;
18         ifile.seekg(0,ios::beg);
19 }
20 ifile.close();
21 }

输出:

this is 0 file
hello this is testing fstream!
eof: 0
this is 1 file
hello this is testing fstream!
eof: 0
this is 2 file
hello this is testing fstream!
eof: 0

免责声明:内容来源于网络,仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇VC终止线程,ExitThread函数,Te r m i n a t e T h r e a d函数,撤消线程,详解优化笔记:pfyhparopenfundinfotest_D_20140916.gz下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

c++读写二进制文件

要读取文件必须包含<fstream>头文件,这里包含了C++读写文件的方法,可以使用fstream 类,这个类可以对文件进行读写操作。   1、打开文件。   打开文件可以有两种方式,第一种可以使用fstream 类的构造函数。 ios::in :输入文件(同ios_base::in) ios::out :输出文件(同ios_base::out...

C/C++文件输入输出操作——FILE*、fstream、windowsAPI

基于C的文件操作在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O文件操作,下面就分别介绍之。 一、流式文件操作这种方式的文件操作有一个重要的结构FILE,FILE在头文件stdio.h中定义如下: typedef struct {int level;unsigned flags;char fd;unsigned char hold;int...

C++标准库之IO库(一)

概述 C++语言与C语言一样,语言本身并不提供输入输出的支持,它们实现输入输出都是通过标准库来完成的。C语言的标准库提供一系列可以用来实现输入输出的函数,C++标准库则提供一系列类和对象来完成输入输出的功能,并且提供了流的概念,标准库中的IO类都是流概念的类。C++标准库中80%的内容属于STL,而IO库并不属于这80%。IO库体现的是面向对象的思想,但是...

C++ fstream文件读取操作

1.在头文件fstram中,定义了了三个类型:ifstream用来从一个给定文件中读取数据,ofstream向一个给定文件写入数据,fstream读写指定文件。 2.fstream是iostream的一个基类,所以我们也可以使用<<、>>、getline等来操作fstream 3.使用>>从文件中读取数据,和从控制cin...

Jmeter之 forEach 遍历所有值

今天遇到一个场景,通过正则表达式提取多个值,然后保存到文件中去 正则表达式 1、一开始的思路是通过后置 Beanshell断言进行,得到文件数据为 null FileWriter fstream=new FileWriter ("D://test.csv",true); BufferedWriter out=new BufferedWriter(fstre...

C++文件fstream的操作

                用到的关于输入输出fstream流相关的知识 1.两个主要函数:read( )函数 从流中读取字符串的成员函数read 该成员函数一般形式是:read(char* pch, int nCount) 从输入流中读取nCount个字符。当输入流中的字符数小于nCount时,结束读取。经常使用read函数读取二进制数据。wri...