Like Tree4Likes
  • 3 Post By rags_to_riches
  • 1 Post By iMalc

Difference between seekg() and seekp()

This is a discussion on Difference between seekg() and seekp() within the C++ Programming forums, part of the General Programming Boards category; What is the difference between seekg() and seekp(), and tellg() and tellp(), as they always appear to be returning the ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Difference between seekg() and seekp()

    What is the difference between seekg() and seekp(), and tellg() and tellp(), as they always appear to be returning the same values ?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    The 'g' version move the get pointer, the 'p' versions move the put pointer... If memory serves, a particular implementation may use a single pointer for both. However if you are about to perform input you should definitely use seekg(), and seekp() in the other case.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    1
    seekg moves the file input pointer(position of reading frm file) while seekp moves file output pointer( position f writing to file).

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Another question from juice answerable simply by reading the documentation.

    istream::seekg - C++ Reference
    ostream::seekp - C++ Reference

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by rags_to_riches View Post
    Another question from juice answerable simply by reading the documentation.
    No one really understood the question, or maybe I was unable to express myself, but I let it go, assuming(from what I observe from a couple of programs) that seekg and seekp can be used interchangeably.

    And f** you, by the way.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    And had you read the documentation you would have seen that seekg and seekp can not be used interchangeably....

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by juice View Post
    No one really understood the question, or maybe I was unable to express myself, but I let it go, assuming(from what I observe from a couple of programs) that seekg and seekp can be used interchangeably.
    And you did not understand that observing the side effects of something opaque is not the best way to understand it, the documentation is.

    Seriously, what makes you think that they can be used interchangeably ?
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by manasij7479 View Post
    Seriously, what makes you think that they can be used interchangeably ?
    This is what makes me think so...

    Code:
    #include"stdafx.h"
    #include<iostream>
    #include<fstream>
    
    struct record
    	{
    		char  code[6];
    		char name[20];
    		int i;
    	}r;
    
    int main()
    {
    	std::fstream file("Temp.dat",std::ios::trunc|std::ios::in|std::ios::out|std::ios::binary);
    	if(!file)
    	{
    		std::cout<<"unable to open file";
    		exit(0);
    	}
    
    	std::cout<<"enter character code, name and an int\n";
    
    	std::cin.getline(r.code,6);
    	std::cin.getline(r.name,20);
    	std::cin>>r.i;
            file.write((char *)&r,sizeof(r));
    
    	std::cout<<"\n\n"<<file.tellg()<<'\n'<<file.tellp();
    	
    	file.seekg(3);
    	std::cout<<"\n\n"<<file.tellg()<<'\n'<<file.tellp();
    
    	file.seekp(5);
    	std::cout<<"\n\n"<<file.tellg()<<'\n'<<file.tellp();
    
    }
    you really think I would like to post a question and wait for someone to answer when I could simply get an answer from some documentation?

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Just because seekg and seekp may operate on the same pointer (the get and put pointer) they are not guaranteed to do so, which you would have realized had you read the two links posted to you....

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by juice
    This is what makes me think so...
    By that same logic, writing outside of the bounds of an array must be okay because this works (for me):
    Code:
    #include <stdio.h>
    int main(void) {
        int a[10];
        a[12] = 1;
        printf("%d\n", a[12]);
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Ah, the familar sequence of steps of someone who does not really want to learn...

    Asks Question instead of looking it up.
    Be given correct answer anyway.
    Ignores answer and decides their own wrong answer is correct.
    rags_to_riches likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about seekg
    By ovid in forum C++ Programming
    Replies: 0
    Last Post: 06-09-2010, 06:43 AM
  2. How to replace words in a text file with seekp
    By ishould in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2009, 07:17 PM
  3. seekp and binary files
    By simonc2 in forum C++ Programming
    Replies: 1
    Last Post: 01-19-2005, 12:30 PM
  4. Not sure if I should be using seekg()
    By manofsteel972 in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2004, 08:39 PM
  5. seekp(), seekg()
    By Dual-Catfish in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2002, 11:27 AM

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