Solved

fgets in Borland C

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

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 and use pointers 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.
Suggested Courses

738 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