Thread: Proper use of seekg

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    Proper use of seekg

    I am trying to change the position stream pointer for input to the beginning of a file using
    Code:
        encodedfile.clear();
        encodedfile.seekg(0);
    but after printing out encodedfile.tellg() afterwards, the pointer stays at -1 and I can't read any of the text inside the file.

    Here is the declaration of the variable
    Code:
    std::fstream encodedfile(encodedfilename,std::fstream::in | std::fstream::out);
    and this is where text is outputted to the file
    Code:
    while(file >> std::noskipws >> ch)
        {
            encodedfile << hasbinary[(unsigned int)ch].binaryencoding;
            cout << hasbinary[(unsigned int)ch].binaryencoding;
        }
    Is there another way to return the stream pointer to the beginning or is there a way to fix my code?
    Last edited by workisnotfun; 04-28-2013 at 05:48 PM.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    I also tried closing the file and reopening it with a separate variable but no good. I have no idea what's going on.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post the smallest possible complete program that illustrates your problem.

    You probably should specify the "way" as well.

    Code:
    is.clear();
    is.seekg (0, is.beg);

    Jim

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, 05:43 AM
  2. seekg() problem
    By Rustik_ in forum C++ Programming
    Replies: 6
    Last Post: 09-04-2009, 12:31 AM
  3. Not sure if I should be using seekg()
    By manofsteel972 in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2004, 08:39 PM
  4. tellg and seekg
    By Zoalord in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2004, 02:45 PM
  5. seekg woos
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-26-2002, 08:51 PM