Solved

fgets in Borland C

Posted on 1997-02-28
1
392 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
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now