Sequential file ( reset the file pointer )

I am using pure C++ in UNIX platform.

How can I reset the file pointer to the beginning or anywhere
in the file ?

For example, the file pointer is at EOF, but I want to reset the pointer to the beginning of the file without close it and reopen
it.

Caution : I must use sequential file.
javas08Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
Use the C Run-Time library function fseek.

int fseek(FILE *stream, long offset, int origin);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
That is true if you are using the old C file stream procedures.   (Which I would not recommend.)  If you are using te C++ file stream objects (fstream), you should use the seekg() member procedure.
0
javas08Author Commented:
How to use seekg() ? Can I use it for sequential file ?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

nietodCommented:
>> How to use seekg()
It is almost the same as fseek()

You specify two parameters.  The first is the numerical value to seek to and the 2nd in the "way" in which to seek.  

The way can be "ios_base::beg" to seek from the start of the file. i.e. the file pointer will be positioned at the offset you specify in the first parameter.

The way can be "ios_base::cur" to seek from the current position.  i.e. the fiel pointer will be moved forward the number of elements (usually bytes) specified by the 1st parameter.

the way can be "ios_base::end" to seek backwards  from the end position.  ie. the file pointer wil be mobed to the specified element from the end.  (specify 0 in the 1st parameter to seek to the end of the file.).

>> Can I use it for sequential file ?
It can be used on any stream that supports random access, like string streams or file streams for disk files.
0
javas08Author Commented:
can I write seekg(infile,0,0) ?
And what library should I include ?
0
chensuCommented:
No. Check out your C++ documentation.

#include <fstream.h>

void main()
{
   char ch;

   ifstream tfile( "payroll", ios::binary | ios::nocreate );
   if( tfile ) {
      tfile.seekg( 8 );      // Seek 8 bytes in (past salary)
      while ( tfile.good() ) { // EOF or failure stops the reading
         tfile.get( ch );
         if( !ch ) break; // quit on null
         cout << ch;
      }
   }
   else {
      cout << "ERROR: Cannot open file 'payroll'." << endl;
   }
}

What functions are you currently using to access the file?
0
javas08Author Commented:
I used this to open the sequential file :

ifstream infile;

infile.open(filename);

And I openned it as normal file.
0
chensuCommented:
Then use infile.seekg. See my previous comment for an example.
0
javas08Author Commented:
No. it is no working.
The pointer still at the end of the file.
0
nietodCommented:
You must be doing something wrong.  Why don't you post your code, it may help us to find it.
0
javas08Author Commented:
#include <fstream.h>
 
void main()
{
  char ch;
  ifstream tfile( "payroll");
  tfile >> ch;
  while(!tfile.eof())
 {
   cout << ch<<endl;
   tfile >> ch;
  }
  tfile.seekg(-5);      // Seek 8 bytes in (past salary)
  tfile >> ch;
  cout << ch<<endl;
 }
0
nietodCommented:
If you don't specify the 2nd parameter t seekg(), it default to seeking from the start.  So

>>  tfile.seekg(-5);      // Seek 8 bytes in (past salary)

seeks 5 characters BEFORE the start, which is not allowed..

If you want to seek past the 1st 8 characters do

tfile.seekg(8)

or

tfile.seekg(8,ios_base::beg);
0
javas08Author Commented:
what if I want to set the pointer back to the 1st /beginning of the file ?
0
javas08Author Commented:
it is still not working !

in payroll, I have :
a
b
c
d
e
f
g

Then in the source file, I have :

#include <fstream.h>
#include <iostream.h>
void main()
{
     char ch;
     ifstream tfile( "payroll");
     tfile >> ch;
     while(!tfile.eof())
     {
        cout << ch<<endl;
        tfile >> ch;
      }
      tfile.seekg(1,ios::beg); // if I did iso_base, I got error.
      tfile >> ch;
      cout << ch<<endl;
}

and the output is :

a
b
c
d
e
f
g
g

I think I should get :

a
b
c
d
e
f
g
b

right ?
0
nietodCommented:
The main problem is that you've reached the end of the file so the stream's eof bit is set.  This prevents additional attempts at input from succeeding, so the attempt to read that character after seeking fails and so the chracter retains its previous value (the last character read.)

If you clear the eof bit using

tfile.clear();

it will work correctly.

Now keep in mind that the file you seem to have typed has additional character.  After each character there appears to be a newline, right?  That newline will be "made" up of 1 or two characters.   So you must consider these in the "math" when figuring out how far to seek.  if your newline is two characters (like in DOS/windows), then seeking to 0 is A, to 1 is a CR, to 2 is a LF, to 3 is a B and so on

Also if you seek around in a file, you probably should open the file in binary mode.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.