# C++ question re: reading input from a file

**URL:** <https://boards.straightdope.com/t/c-question-re-reading-input-from-a-file/254968>\
**Category:** Factual Questions\
**Created:** [July 13, 2004, 10:14pm UTC](https://boards.straightdope.com/t/c-question-re-reading-input-from-a-file/254968 "2004-07-13T22:14:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enright3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/enright3/32/2897_2.png) [@Enright3](https://boards.straightdope.com/u/Enright3)\
**Post date:** [July 13, 2004, 10:14pm UTC](https://boards.straightdope.com/t/c-question-re-reading-input-from-a-file/254968/1 "2004-07-13T22:14:29Z")

</div>

OK, I’m a C++ newbie, but I’m trying to learn.  
I have a question. I just finished reading a file using ifstream, and now I want to read it again (code snippet below). I thought I could reset to the beginning of the file by using the seekg( 0 ) method. This doesn’t appear to work.

Does anyone have any suggestions or comments?

```auto

  // read in a file of phrases.
  tfile.seekg(0); // reset data file;
  while (!tfile.eof()) // loop as long as there are items left
  {
    tfile.getline(buffer, 49); // get an item from the file
    if (!tfile.eof())
        phrases.push_back(buffer);
  }//end of while loop

```

---

<div class="post-metadata">

**Author:** ![Bytegeist](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bytegeist](https://boards.straightdope.com/u/Bytegeist)\
**Post date:** [July 13, 2004, 10:30pm UTC](https://boards.straightdope.com/t/c-question-re-reading-input-from-a-file/254968/2 "2004-07-13T22:30:27Z")

</div>

You might need to call the stream’s **clear** method after encountering an error. (Reaching the end-of-file counts as an error.)

See some more discussion in section 9.3.4 of [this page](http://www.eng.usyd.edu.au/tutorial/course1/cplusplus09.html).

---

<div class="post-metadata">

**Author:** ![Pleonast](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pleonast/32/1183_2.png) [@Pleonast](https://boards.straightdope.com/u/Pleonast)\
**Post date:** [July 13, 2004, 10:37pm UTC](https://boards.straightdope.com/t/c-question-re-reading-input-from-a-file/254968/3 "2004-07-13T22:37:20Z")

</div>

You are calling **seekg** incorrectly. The absolute position (one parameter) form is only appropriate with values obtained from **tellg**. Instead you want to use the relative position form (two parameters). Specifically:

```auto

tfile.seekg(0,std::ios::beg);

```

You can also position relative to the current location ( **std::ios::cur** ) or the end ( **std::ios::end** ).

---

<div class="post-metadata">

**Author:** ![Enright3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/enright3/32/2897_2.png) [@Enright3](https://boards.straightdope.com/u/Enright3)\
**Post date:** [July 13, 2004, 11:12pm UTC](https://boards.straightdope.com/t/c-question-re-reading-input-from-a-file/254968/4 "2004-07-13T23:12:37Z")

</div>

> [@Bytegeist](#):
>
> You might need to call the stream’s **clear** method after encountering an error. (Reaching the end-of-file counts as an error.)
> 
> See some more discussion in section 9.3.4 of [this page](http://www.eng.usyd.edu.au/tutorial/course1/cplusplus09.html).

Thanks, I’ll give it a shot. I like that tutorial BTW. It’s bookmarked now.

E3

---

<div class="post-metadata">

**Author:** ![Enright3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/enright3/32/2897_2.png) [@Enright3](https://boards.straightdope.com/u/Enright3)\
**Post date:** [July 13, 2004, 11:15pm UTC](https://boards.straightdope.com/t/c-question-re-reading-input-from-a-file/254968/5 "2004-07-13T23:15:32Z")

</div>

> [@Pleonast](#):
>
> You are calling **seekg** incorrectly. The absolute position (one parameter) form is only appropriate with values obtained from **tellg**. Instead you want to use the relative position form (two parameters). Specifically:
> 
> ```auto
> 
> tfile.seekg(0,std::ios::beg);
> 
> ```
> 
> You can also position relative to the current location ( **std::ios::cur** ) or the end ( **std::ios::end** ).

Oh, I get it! When I was reading the manual on the two forms, I didn’t understand what beg, cur, end meant :smack: . Now it seems so simple.

My work around was just to create a second vector when I created the first, but I still hated not knowing why I couldn’t get it to work.

Thanks, everybody.

E3
