fstream position

This is a discussion on fstream position within the C++ Programming forums, part of the General Programming Boards category; Please take a look at this Code: fstream* file1 = new fstream("C:\Testfile.sia", ios_base::out); file1->close(); file1->open("C:\Testfile.sia"); cout << file1->tellp() << endl; ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    Question fstream position

    Please take a look at this
    Code:
    fstream* file1 = new fstream("C:\\Testfile.sia", ios_base::out);
    file1->close();
    file1->open("C:\\Testfile.sia");
    cout << file1->tellp() << endl;
    cout << file1->tellg() << endl;
    file1->write("somethingsinlifetheyjustdon'wannasee",30);
    file1->seekp(10);
    file1->seekg(5);
    cout << file1->tellp() << endl;
    cout << file1->tellg() << endl;
    file1->close();
    What is the difference between seekp() and seekg()? They seem to always moving together.
    Quote Originally Posted by MSDN
    tellg(): Reports the current read position in the stream.
    tellp(): Report position in output stream.
    seekp(): Reset position in output stream.
    seekg(): Moves the read position in a stream.
    [edit] I was ... . I just don't know why they move together.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    I'm not sure why MSN has it explained like that. It's not incorrect, but perhaps confusing.

    seekg sets the get pointer of the stream to a new position.
    seekp sets the put pointer of the stream to a new position.

    seekg is in fact istream::seekg
    seekp is in fact ostream::seekp
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    But why they are always in the same position?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Well conceptually they are not. But in practice they are. The thing is that despite opening a file for read and write access there is only one marker to that buffer. Both seekg and seekp point to this marker.

    Both functions (and the accompanying tell functions) make only sense to be used together when you open a stream for reading and writing and that stream allows for random access.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Both functions (and the accompanying tell functions) make only sense to be used together when you open a stream for reading and writing and that stream allows for random access.
    So what is their purpose?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    You can read and write to the same stream without needing to close and reopen it each time. You read from the stream starting from positions you indicate with seekg and you write to positions you indicate with seekp.

    fstream is a random access stream. You can open an with with fstream::in | fstream:: out. seekp and seekg will allow you to freely navigate the stream up and down both for reading and writting.

    Of course, you cannot expect to make any sense of the value tellg or tellp return. These functions return a streampos object. This class is used by the compiler (and is compiler specific) in order to establish the positions in the stream. But otherwise it has no meaning to us humans. You can add or subtract two streampos objects. This allows you to do relative searches in addition to random ones.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    What I mean is if they both point to one thing, isn't one of them enough?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    I'm sure. But if they are implemented differently there is certainly a very good reason to. The answer lies definitely in the way streams are implemented. Probably something to do with input and output sequences.

    I cannot help you much further than this. I simply don't know past what I already told you. I tend not to ask many questions on how this or that is implemented. I simply accept it and move on. May seem as the antithesis of the learning process. But it serves me well. When the programming language is well known to me and I'm well adapted at coding complex applications with it, then definitely I will be more interested in knowing how it all was put together.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21