Thread: How can I know the postion of reading in fstream

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    40

    How can I know the postion of reading in fstream

    Please , If I have a read_file object of fstream, and I want to read the postion that I am standing on without reading it . do I ise peek() ? I tried to use it , the first read returned 91 and the next returned 81 .

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    int main(){
       int x=0;
       ifstream bin_file("c:\\compiler_test\\bin.txt");
       x = bin_file.peek();
       cout << "the first read = " << x << endl;
      bin_file.get();
      x = bin_file.peek();
      cout << "the second read = " << x;
    
      return 0;
    
    }
    thank you.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    "read the position without reading it"? You mean changing.

    http://msdn2.microsoft.com/en-us/lib...17(VS.80).aspx
    peek reads a character without advancing the read position. But the bin_file.get() that you have between the two peeks does.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    yes, exactly !

    but how can I advance the postion ?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, get(), getline(), read(), they all advance the position. ignore() does it without returning the data. seek() (or seekg() and seekp(), to move read and write position separately for those streams that support it) can be used too; it's especially good for large jumps.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    good , but if I want to return the read postion back one char ?
    Last edited by GSalah; 11-09-2006 at 06:43 AM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    tell() to save the current position, seek() to restore it. Don't you have a reference to look this stuff up?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Reading from a file using fstream.
    By sbho in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2007, 11:14 AM
  3. I need some help reading in data with fstream
    By Geo-Fry in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2003, 08:25 AM
  4. Does reading file with fstream not work on xp?
    By pinkcheese in forum C++ Programming
    Replies: 13
    Last Post: 04-11-2002, 08:39 AM
  5. Reading Files with fstream
    By pinkcheese in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2002, 11:10 PM