Which Statement Move The File Pointer To The Last Character

Posted onby
  • Selected Reading
C - Working with Files

Learning C
C Function References
C Useful Resources
Selected Reading

Copyright © 2014 by tutorialspoint

HomeReferencesAbout TPAdvertising


When accessing files through C, the first necessity is to have a way to access the files. For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. For Example:

To open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file.

Here filename is string literal which you will use to name your file and mode can have one of the following values

Note that it's possible for fopen to fail even if your program is perfectly correct: you might try to open a file specified by the user, and that file might not exist (or it might be write-protected). In those cases, fopen will create a file if you specify file mode as 'w', 'w+', 'a', or 'a+' otherwise it will return 0, the NULL pointer.

Here's a simple example of using fopen:

This code will open test.txt for reading in text mode. To open a file in a binary mode you must add a b to the end of the mode string; for example, 'rb' (for the reading and writing modes, you can add the b either after the plus sign - 'r+b' - or before - 'rb+')

To close a function you can use the function:

fclose returns zero if the file is closed successfully.

An example of fclose is:

To work with text input and output, you use fprintf and fscanf, both of which are similar to their friends printf and scanf except that you must pass the FILE pointer as first argument.

Try out following example:

Thsi will create a file test.txt in /tmp directory and will write This is testing in that file.

Here is an example which will be used to read lines from a file:

It is also possible to read (or write) a single character at a time--this can be useful if you wish to perform character-by-character input. The fgetc function, which takes a file pointer, and returns an int, will let you read a single character from a file:

The fgetc returns an int. What this actually means is that when it reads a normal character in the file, it will return a value suitable for storing in an unsigned char (basically, a number in the range 0 to 255). On the other hand, when you're at the very end of the file, you can't get a character value--in this case, fgetc will return 'EOF', which is a constnat that indicates that you've reached the end of the file.

The fputc function allows you to write a character at a time--you might find this useful if you wanted to copy a file character by character. It looks like this:

Note that the first argument should be in the range of an unsigned char so that it is a valid character. The second argument is the file to write to. On success, fputc will return the value c, and on failure, it will return EOF.

There are following two functions which will be used for binary input and output:

Both of these functions deal with blocks of memories - usually arrays. Because they accept pointers, you can also use these functions with other data structures; you can even write structs to a file or a read struct into memory.


  • Restriction:The trailing @ or double trailing @ must be the last item in the PUT statement. Tip:Use an @ or @@ to hold the pointer at its current location. The next PUT statement that executes writes to the same output line rather than to a new output line.
  • When you have multiple cursors use the vim command f followed by a character to move all the cursors to the next find of this character. Unfortunately these only move to characters on the same line and cursors on lines without this character are left where they previously were located.

File Pointer In C

Aeosynth commented Nov 28, 2012. In vim, the cursor cannot move beyond the last character of a line; in CodeMirror's implementation (even the rewrite), this is possible. Example: In the first line, the cursor can move one past the final '; in vim, the cursor would end at the final '. Fseek() function is used to move file pointer position to the given location. Where, fp – file pointer. Offset – Number of bytes/characters to be offset/moved from whence/the current file pointer position. Whence – This is the current file pointer position from where offset is added. Below 3 constants are used to specify this.

Active8 years, 3 months ago
Which Statement Move The File Pointer To The Last Character

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?

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


An Attempt Was Made To Move The File Pointer

7,69947 gold badges158 silver badges295 bronze badges

4 Answers

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 YorkMartin YorkHow to move the file pointer in c

Which Statement Move The File Pointer To The Last Character

206k69 gold badges275 silver badges495 bronze badges

How To Move The File Pointer In C

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

Which Statement Move The File Pointer To The Last Characteristics

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.

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.