0

I was reading C File Handling and I ran into fseek() function. The following statement was written in the book, regarding the use of it:

int fseek(FILE *stream, long offset, int from );  //The function prototype

"On binary streams, seeks from SEEK_END may not be supported an should therefore be avoided. On text streams, the offset must be zero if from is either SEEK_CUR or SEEK_END. The offset must be a value previously returned from a call to to ftell() on the same stream if from is SEEK_SET."

I don't understand the given usage.Why the offset should be zero ?

To find the answer, I investigated and I found out that, in text streams, there is mapping of EOL(newline) from C program to different character in MSDOS. The size of newline character in C is 1 byte.
What happens when it gets written to a Notepad file?. What is the size of EOL in notepad?
I created a notepad file and did the following:

Scenario 1:
abcd

The size shown was 4 bytes. Where is newline or EOF now?

Scenario 2:
abcd
a

The size shown was 7 bytes.

Scenario 3:
abcd
a
b

The size shown was 10 bytes. How was the size calculated now?

Can anybody answer these questions?

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105

2 Answers2

1

I assume notepad adds windows like end of line which consists of \r\n hence the answer to your question is 2, and that obviously explains the observed behavior.

Also, EOF is not a character written to the file, it's a special value returned by some funcitions to indicate the end of the file.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

Notepad uses DOS / Windows sequence of \r\n to mark end-of-line.

  • First example has no end-of-line sequence, so 4 printable characters produce size of 4: a b c d
  • Second example has one end-of-line sequence, so 5 printable characters plus 2 for the end-of-line marker make its size 7: a b c d \r \n a
  • Third example has two end-of-line sequences, so 6 printable + 4 for two end-of-line markers make its size 10: a b c d \r \n a \r \n b

You can write a small C program to read your files character-by-character, and print them as numbers. \r's code is 13; \n's code is 10.

Where is the EOF character? Is it not counted in for size calculation?

EOF is not a character, it is a value returned by I/O functions to indicate that the end of input has been reached. It has a special numeric value which is different from numeric values of all other characters. In fact, the reason the functions from getchar family return int, not char, is to accommodate returning EOF marker, instead of reserving one of the character codes for it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Where is the EOF character? Is it not counted in for size calculation? – Manish Kumar Sharma Jan 15 '15 at 15:35
  • Can you tell, who "catches" the EOF value? – Manish Kumar Sharma Jan 15 '15 at 15:45
  • @pulp_fiction I/O functions of your environment (usually, OS-related, but could be something else in embedded systems) "know" the length of file in characters, so after the last character has been read, they return `EOF` value every time your program requests the next character. Console input behaves in a similar way, except console reserves a special key sequence to signal the end of input manually (`Ctrl+D` on UNIX, `Ctrl+Z` on Windows). – Sergey Kalinichenko Jan 15 '15 at 15:52
  • Okay, Correct me if I am wrong, "the OS I/O functions return the EOF, which in turn is returned by C library I/O functions to indicate the end of file." – Manish Kumar Sharma Jan 15 '15 at 15:58
  • @pulp_fiction That's a system-dependent detail of implementation. It can be done like this, but it's also possible for OS I/O functions not to return anything, expecting the C library to keep track of file size (which OS I/O functions provide) and return `EOF` once their internal marker reaches the file size. I/O functions in C library provide an abstraction layer on top of OS I/O functions, hiding these details from you. – Sergey Kalinichenko Jan 15 '15 at 16:05