warning in ubuntu in return value of 'fscanf'

653 Views Asked by At

installing festival speech synthesis system from http://festvox.org/ ...directly by running general script. Facing below given problem .....this problem will effect my working on festival framework or not ?????

eps.c: In function ‘getd’:

eps.c:142:7: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result [-Wunused-result]

   fscanf(fp, "%d %d", x, y);
   ^
3

There are 3 best solutions below

2
On BEST ANSWER

This warning says that not checking the return value of scanf is not a really good idea.

I thought casting to (void) was a way to avoid that but apparently it is not as discussed here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509

However it is not an error that you get but just a warning (your title is kinda misleading)

If the value is not used it is clearly not a problem.

3
On

The man page say:

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set to indicate the error.

This means that you can check against EOF:

#include<stdio.h>

int main(void){
    int a;
    printf("Please give the value of A: ");

    if(scanf("%d",&a) != EOF){
        printf("\nThe value of A is\t%d\n",a);
    }

    return 0;
}

Or:

#include<stdio.h>
#include <errno.h>
#include<string.h>

int main(void){
    int a, errnum = errno;
    printf("Please give the value of A: ");

    if(scanf("%d",&a) == EOF){
        fprintf(stderr, "Value of errno: %d\n", errno);
        perror("Error printed by perror");
        fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
    }

    printf("\nThe value of A is\t%d\n",a);
    return 0;
}

This applies for:

scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf - input format con‐ version

0
On

Read documentation of fscanf(3). You should use the returned count of successfully scanned items, e.g. code something like:

int x = 0, y = 0;
if (fscanf(fp, "%d %d", &x, &y) < 2) {
   fprintf(stderr, "missing numbers at offset %ld\n", ftell(fp));
   exit(EXIT_FAILURE);
}

so you could improve the eps.c file (and perhaps submit a patch and/or a bug report upstream).