O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading from Files

To read from a file in C, you can use the fgets() function. It was first introduced in Chapter 5 as a reliable way to read numeric input from the keyboard. The syntax for using fgets() is

fgets (var, length, pointer);

The function reads up to length amount of data from the file and assigns this to var. You should take precautions to ensure that the function does not attempt to read in more data than can be stored in the variable. A reliable way to do this is to use the sizeof() operator:

fgets (var, sizeof(var), pointer);

Line and File Endings

One of the nice things about C with respect to working with files is that it will automatically convert line endings. Different operating systems use different characters to mark ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required