C/C++ declared with attribute warn_unused_result

I want to read a text file with C. This is my simple ilustration code :

FILE *offsetFile=NULL;
char *buffer;
int cbyte;

cbyte = 100;
buffer = (char*) calloc (cbyte, sizeof(char));
offsetFile = fopen("listOffset.txt", "r");
if (offsetFile==NULL)
{
    exit(0);
}
fgets (buffer, cbyte, offsetFile);

When I compile this code I get warning message :

ignoring return value of ‘fgets’, declared with attribute warn_unused_result

This is because functions fgets returning char. To solve this problem, I change my code with :

FILE *offsetFile=NULL;
char *buffer;
char *creturn;
int cbyte;

cbyte = 100;
buffer = (char*) calloc (cbyte, sizeof(char));
offsetFile = fopen("listOffset.txt", "r");
if (offsetFile==NULL)
{
    exit(0);
}
creturn = fgets (buffer, cbyte, offsetFile);
Source :
http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/fgets.html
http://www.codecogs.com/reference/computing/c/stdio.h/fgets.php
2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *