by Dinesh Thakur Category: File Handling

To read a string in a file we can use fgets () whose prototype is:

char * fgets (char * str, int size, FILE * fp);

The function takes three arguments: the string to be read, the maximum limit of characters to read and the pointer to FILE, which is associated with the file where the string is read. The function reads the string until a newline character is read or size-1 characters have been read. If the newline character ('\n') is read, it will be part of the string, which did not happen with gets. The resulting string will always end with '\0' (for this only size-1 characters maximum, will be read).

The fgets function is similar to gets (), however, beyond power to read from a data file and include the newline character in the string, it also specifies the maximum size of the input string. As we have seen, the gets function had this control, which could lead to errors of "buffer overflow". Therefore, taking into account that the fp pointer can be replaced by stdin, as we saw above, an alternative to the use of gets is to use the following construction:

                 fgets (str, size, stdin);

where str and 'the string that you are reading and size should match the size allocated for the string minus 1 because of' \0 '.



                 char * fputs (char * str, FILE * fp);

Write a string to a file.

Ferror and perror

Prototype ferror:

int ferror (FILE * fp);

The function returns zero if no error occurred and a number other than zero if an error occurred while accessing the file.

ferror () becomes very useful when we want to ensure that each access to a file succeeded, so that we can ensure the integrity of our data. In most cases, if a file can be opened, it can be read or written. However, there are situations where this does not occur. For example, you end up disk space while recording, or the disc may be faulty and can not read, etc.

A function which can be used in conjunction with ferror () is the perror () function (print error), whose argument is a string which typically indicates that the problem of the program has occurred.

In the following example, we use ferror, perror and fputs

#include <stdio.h>

#include <stdlib.h>
void main ()
     FILE * pf;
     char string [20];
     if ((pf = fopen ("Hello.txt", "w")) == NULL)
         printf ("\ Program can not open the file!");
         exit (1);
             printf ("\nPlease enter a new string Finally, type <enter>:.");
         gets (string);
         fputs (string, pf);
             putc ('\n', pf);
         if (ferror (pf))
             perror ("Error writing");
             fclose (pf);
             exit (1);
     } while (strlen (string)> 0);
        fclose (pf);

C Program to use ferror, perror and fputs

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.