Solved

fgets in Borland C

Posted on 1997-02-28
1
406 Views
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().
0
Comment
Question by:pneves
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
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>.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Estimate porting issues. 9 122
Coverting 24 hour time to 12 hour in C++ 15 182
chcp 65001 File encoding 66 299
List out all word 7 336
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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

749 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