Does fgets() move the file pointer?
Asked Answered
C

2

8

Does the fgets() function move the file pointer automatically to the position till the parameter of the size mentioned by me?

for example:

the content of the file p.txt is " I am a good boy ". After using fgets(a, 5, fp1) does the file pointer move 5 positions ahead?

Conscious answered 5/5, 2017 at 16:36 Comment(5)
The FILE* is not modified by fgets(). The file offset may be (if the file is seekable).Alphonse
I wonder why you ask. Have you some code that indicates otherwise? There must be some background for this question. Please explain.Mylohyoid
@SamS - why would you think a question about fgets() is a duplicate of a question about fread()?Duleba
the reason is i want to copy one file to the other . so the question that arose in my mind that if the pointer does not move , the string variable in my fgets() argument will have the same string .Conscious
@ArkaPravaPaul - The file position indicator is changed when doing fgets. Successive fgets will not return the same data. No problem.Mylohyoid
N
7

after using fgets(a,5,fp1) does the file pointer move 5 positions ahead ?

The pointer fp1 is not affected by the fgets call (or any other stdio I/O routine); The FILE object that fp1 points to will be updated to reflect the new file position, but the pointer itself does not change.

Nissensohn answered 5/5, 2017 at 19:27 Comment(4)
Could you recommend me some C references/books ? .. I could not find about this in any particular book .Conscious
@ArkaPravaPaul: I've been using Harbison & Steele's C: A Reference Manual since the late '80s (currently 5th edition, covers up through C99). I've also heard good things about King's C Programming: A Modern. Approach, although I don't have personal experience with it. And you should bookmark the latest online draft of the C language standard.Nissensohn
This is the most crucial detail about the function and no cpp reference, man page or website states the basic fact that there's a file position that's incremented, they just make you work it out for yourself – it's obvious from the output, and the fact the function would be useless without it because there's no offset parameter so you'd always be reading from the start of the file and wouldn't be able to read past a new line. I don't know why these sites don't state it explicitly, then questions like this wouldn't be askedMexicali
@LewisKelsey: Because a stream doesn't have to be tied to a file, and because it's a ridiculously low-level detail that the stdio library was explicitly designed to hide from you. You call fgets, magic happens, and data appears in the target buffer.Nissensohn
F
3

The file pointer is not modified by the fgets function.

However, the file offset is incremented by the number of bytes actually read.

Favorite answered 5/5, 2017 at 16:41 Comment(1)
You should emphasize that "the number of bytes read" is NOT equal to the second argument to fgets; it will be at most one less than that value, and may be smaller (if you hit EOF or a newline or read error).Cavernous

© 2022 - 2024 — McMap. All rights reserved.