1

I simply want to check whether a value which is present in a variable is integer or not.

My program below reads two values from "myfile.txt" and performs addition if and only if they are integers otherwise it returns "Invalid Output". I have successfully read values from "myfile.txt" but i have no way of checking whether the two values are integer or not.How to perform this test?

#include <stdio.h>
#include <conio.h>

int main()
{
    FILE *fp;
    int i, mkji, num, num1, num2;
    int s = 0;
    int a[2];
    char term, term2;
    clrscr();
    fp = fopen("myfile.txt", "r+");
    if (fp != 0)
    {
        for(i = 0; i < 2; i++)
        {
            fscanf(fp, "%d", &a[i]);
        }
        for(i = 0; i < 2; i++)
        {
            printf("%d\n", a[i]);
        }
        num = a[0];
        num1 = a[1];
        num2 = num + num1;
        printf("%d", num2);
        mkji = fclose(fp);
    }
    else
        printf("FILE CANNOT BE OPEN");
    getch();
    return 0;
}
nalzok
  • 14,965
  • 21
  • 72
  • 139
Anidh Singh
  • 302
  • 2
  • 7
  • 19
  • If you really want to know for c++ as tagged, one duplicate is [How to test whether stringstream operator>> has parsed a bad type and skip it](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it). For c you may check the return value of `fscanf()`. – πάντα ῥεῖ Feb 20 '16 at 12:32
  • `fscanf` returns an error if it cannot read an integer from the input file. So, always check the return value. I think, it must also read `num2 = num+num1` or just `num2 = a [0] + a [1]`. – Martin Zabel Feb 20 '16 at 12:35
  • @MartinZabel corrected it :) – Anidh Singh Feb 20 '16 at 12:40
  • 1
    Possible duplicate of [Check if a value from scanf is a number?](http://stackoverflow.com/questions/2023079/check-if-a-value-from-scanf-is-a-number) – fuz Feb 20 '16 at 12:40
  • 1
    C does not support dynamic typing. So the type of a variable is known at compile time. – too honest for this site Feb 20 '16 at 12:59

3 Answers3

1

Check the return value of fscanf. fscanf returns the number of succesfully scanned items or -1 if there was an IO error. Thus, if the number was malformed, fscanf("%d",&a[i]); should return 0.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • @AnidhSingh No. It returns 0 as outlined in the last sentence. The best way is to check if the return value is equal to 1. If it is, you successfully scanned a number. If not, some sort of error occured or there was no number. – fuz Feb 20 '16 at 12:34
  • oh.. so if fscanf reads a NUMBER it returns 1 else some error occured? – Anidh Singh Feb 20 '16 at 12:36
  • @AnidhSingh Yes. `fscanf` returns the number of items successfully scanned. `%d` is a directive. If a number was found, the item for `%d` was successfully scanned, thus one item was successfully scanned, thus 1 is returned. Please read the manual page for `fscanf` for more details. – fuz Feb 20 '16 at 12:38
  • Thanks. It was very helpful – Anidh Singh Feb 20 '16 at 12:41
0

Read the file as ASCII character and check for their ASCII Codes. If the ASCII lies in the range of a number 0 number has ASCII 48 and 9 number has ASCII 57. so each number lies between ASCII 48 to 57.

you can concatenate them in a string since you are reading characters until a space appears and once you get 2 numbers just add them :)

code can be in the form of

char[] number1;
char numRead;
keep reading the number using fscanf until a non-number appears.
if ( numRead >= '0' && num <= '9' ) // checking for if it is a number.
   number1 += numRead; // atleast thats how you do it in C++
Martin Gardener
  • 1,001
  • 8
  • 14
  • I'm not sure what this is supposed to do. If I use `fscanf` to scan the number `100`, surely your check would give an incorrect indication? – fuz Feb 20 '16 at 12:39
  • thats what i said, if there are a series of numbers keep concatenating them until a space appears, 1st iteration '1' read. 2nd iteration '0' read. 3rd iteration '0' read. 4rth iteration ' ' read. concatenate them into 1 result '100'. If this was C++ i would show you how to code it since i haven't touched C for like 4/5 years i forgot how to deal with this type of problems. – Martin Gardener Feb 20 '16 at 12:41
0

check the return value from fscanf()

it can return -1 for a non number
and 0 or 1 for a number.

Danyal Imran
  • 77
  • 11