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;
}
5
  • 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. For c you may check the return value of fscanf(). Commented Feb 20, 2016 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]. Commented Feb 20, 2016 at 12:35
  • @MartinZabel corrected it :) Commented Feb 20, 2016 at 12:40
  • 1
    Possible duplicate of Check if a value from scanf is a number? Commented Feb 20, 2016 at 12:40
  • 1
    C does not support dynamic typing. So the type of a variable is known at compile time. Commented Feb 20, 2016 at 12:59

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@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.
oh.. so if fscanf reads a NUMBER it returns 1 else some error occured?
@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.
Thanks. It was very helpful
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++

2 Comments

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?
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.
0

check the return value from fscanf()

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.