fgets in Borland C

Posted on 1997-02-28
Last Modified: 2006-11-17
Why fgets() does not update properly the file pointer when the file being read was opened in text mode and its respective lines teminate with a single <LF> instead of >CR><LF>? What can be expected to happen and why? I got several different results like fgets returning NULL or wrong value for current file pointer as returned by ftell().
Question by:pneves
1 Comment

Accepted Solution

emmons earned 25 total points
ID: 1249396
According to the book:
The fgets function reads a string from the input stream argument and stores it in string. Characters are read from the current stream position up to and including the first newline character ( \n), up to the end of the stream, or until the number of characters read is equal to n-1, whichever comes first. The result is stored in string, and a null character ('\0') is appended. The newline character, if read, is included in the string. The fgets function is similar to the gets function; however, gets replaces the newline character with NULL.
the newline character ('\n') is defined, on a DOS machine, as <CR><LF>. If you do an fgets() on a text file that was saved with Unix line endings (<LF>) instead of DOS (<CR><LF>) then the fgets() should always read the max number of characters into your buffer.
You can get around this by either converting the file before reading it in (UNIX2DOS.EXE is widely available on the net) or write your own version of fgets() by reading in the data a character at a time and watching for the <LF>.

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question