Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

fgets in Borland C

Posted on 1997-02-28
1
Medium Priority
?
416 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 50 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

926 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