Which Statement Move The File Pointer To The Last Character

Active5 years, 4 months ago

The sequence of bytes starting at the current file pointer position in the file from COMPSCI 160 at University of California, Berkeley.

I'm most often finding myself having to work with plain old vi on minimalistic terminals that tend to act differently than the vim on big distros, and so the behavior trips me up.

What I want to know is not how to move to the last character in the line, but one character past that. Because typing $ does NOT move the insertion cursor to the last character in the line, and this is easily proven. I'm using vi on MSYS right now.

If I type in the line

This is a test

and hit esc, $, i, and <enter>, I will get the following:

This shows that the insertion cursor was not put at the end of the line, but one to the left of the last character in the line (Imagine the box being a line like in Notepad++. The box is over the top of the letter, signifying that the character will be inserted in the area that makes up the box's left side), and I know this because if it were really at the end ('end' meaning to the RIGHT of the last character) the last character would not be moved to its own line such as what you see above.

I don't want this behavior. It's too much a pain for me to type one extra character every time I press enter just to ensure I don't have to retype my words. This kills my productivity.

How To Move The File Pointer In C

How do I fix this? Is it a setting I can tweak in .exrc?

Dylan LaCoursiereDylan LaCoursiere

2 Answers

The normal mode command for entering insert mode at the end of the line is A.

More generally, i enters insert mode before the current character so what you get is perfectly in line with what you do: $ puts the cursor on the last character and i enters insert mode before the last character. That's what you ask Vim to do and that's what it does.

If you want to enter insert mode after the current character, the right command is a so what you should have done instead of $i (which can't do what you want, whether you are in vi or vim) is $a for which there is a cool shortcut: A.

romainlromainl

Fopen

135k14 gold badges197 silver badges230 bronze badges

I am guessing you want to use o to 'open' a new line. However I have provided some ways to insert text via vi

Insert commands

It should be noted that vi/vim's cursor sets on top of a character not between character like most editors.

  • i insert text before the cursor
  • a append text after the cursor
  • I insert text before the first non-blank in the line
  • A append text to the end of a line
  • o begin a new line below the current one and start insert mode
  • O begin a new line above the current one and start insert mode
  • S/cc delete the current line and start insert.
  • c{motion} delete {motion} and start insert mode. Read as change
  • C Delete from current position to end of line and start insert mode
  • s remove character under cursor and start insert mode
  • r replace one character
  • R start replace mode. Think of it as overwrite

For help on any of these command type :h {command} so help on A would be :h A

Peter RinckerPeter RinckerHow to move the file pointer in c
34.7k6 gold badges52 silver badges79 bronze badges

Not the answer you're looking for? Browse other questions tagged vimvieol or ask your own question.

Active8 years, 3 months ago

seekg uses ios as the second argument, and ios can be set to end or beg or some other values as shown here: http://www.cplusplus.com/reference/iostream/ios/

I just want the pointer to move to the next character, how is that to be accomplished through ifstream?

Which Statement Move The File Pointer To The Last Character

EDITWell, the problem is that I want a function in ifstream similar to fseek, which moves the pointer without reading anything.

Aquarius_Girl
Aquarius_GirlAquarius_Girl
7,69947 gold badges158 silver badges295 bronze badges

4 Answers

ybungalobillybungalobill
48.6k13 gold badges102 silver badges168 bronze badges

Yes. Its called seekg() as you seem to already know?

Note this is the same as:

Martin YorkCharacter

Which Statement Move The File Pointer To The Last Character

Martin York
206k69 gold badges275 silver badges495 bronze badges

Read the docs for seekg and use ios_base::cur as indicated there.

MatMat
171k30 gold badges331 silver badges349 bronze badges

I guess peek() and unget() could be useful too.

Use peek() to peek next character so that getline will work as you have wanted.

Use unget to put the character back to your buffer in case you have used get() method.

O.C.O.C.
6,1021 gold badge19 silver badges25 bronze badges

Not the answer you're looking for? Browse other questions tagged c++file-ioifstream or ask your own question.