Thread: Recognize if the floating number is valid or not in C

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Recognize if the floating number is valid or not in C

    I can now validate whether a number is a valid float number or not but i'm having problems in getting the values from a text file. Can someone help me?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    int check_floatnum(double *output)
    {
    	double floatnum;
    	FILE *fp1;
    	char *end, fnum[32];
    
    	if(fgets(fnum, sizeof(fnum), stdin) && !isspace(*fnum) && (*output = strtod(fnum, &end)), ( *end == '\n' || *end == '\0' ))
    		printf("VALID\n ");
    	else
    		printf("INVALID\n ");
    }
    
    int main(void)
    {
    	double floatnum = -12.3;
    
    	do
    	{
    		fp1 = fopen("C:\\input.txt","r");
    
    
    	}
    	while (check_floatnum(fp1));
    
    	fclose(fp1);
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can use fgets to read from a file:
    Code:
    #include <stdlib.h>
    
    int main() {
            float X;
            char fnum[16];
            FILE *file=fopen("text.file","r");
    
            fgets(fnum,16,file);  /* use the file stream pointer instead of stdin */
            X=strtof((const char*)fnum,NULL);
            printf("%s%f\n",fnum,X);
    
            return 0;
    }
    That presumes that the first thing in the file is a decimal number. If you need to parse it out of other data, then things become more complicated.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Thank you but i need to pass the values from file to function check_floatnum correctly. The function check_floatnum validates whether the float number from input.txt is a valid floating point number or not. Now, the problem is i cannot pass the values from input.txt to function check_floatnum correctly. Can you please help me? This is the sample values from input.txt:

    1.15 VALID
    1/2 INVALID


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    int check_floatnum(double *output)
    {
    	double floatnum;
    	FILE *fp1;
    	char *end, fnum[32];
    
    	if(fgets(fnum, sizeof(fnum), fp1) && !isspace(*fnum) && (*output = strtod(fnum, &end)), ( *end == '\n' || *end == '\0' ))
    		printf("VALID\n ");
    	else
    		printf("INVALID\n ");
    }
    
    int main(void)
    {
    	double floatnum = -12.3;
        float X;
        char fnum[16];
    
    	FILE *fp1=fopen("C:\\input.txt","r");
    
    	do
    	{	
    
    
    	}
    	while (check_floatnum(fp1));
    
    	fclose(fp1);
    
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code is a bit of a mess:
    You are passing a FIEL pointer to a function that expects a double pointer. You then USE FILE *fp1 in the function - this variable has not been initialized and I'm 99.9% sure that the random value that it happens to have is not a valid FILE * that points to the same object that you got from fopen() in main - and I guess that's what you'd actually want to do.

    So, first, change the parameter type of your check function so that you pass a FILE * into it [you may need to have a double pointer too, if you need to also pass back the value from the file]. Second, remove the FILE *fp1 from the function, an use the passed in FILE pointer.

    You also do not need to use "isspace" to validate your number - strtod() and friends already do that for you:
    From man strtod(3)
    Quote Originally Posted by Manpagez
    In any of the above cases, leading white-space characters in the string
    (as defined by the isspace(3) function) are skipped. The decimal point
    character is defined in the program's locale (category LC_NUMERIC).
    Code:
    && (*output = strtod(fnum, &end)), ( *end == '\n' || *end == '\0' )
    This is too obfuscated - don't use the comma operator like that. Do you actually know what this does?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. HELP! Changing the base of a floating point number..
    By yigster in forum C Programming
    Replies: 6
    Last Post: 03-27-2008, 05:36 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Replies: 14
    Last Post: 04-06-2006, 12:18 AM
  5. problem printing with floating number
    By ssharish in forum C++ Programming
    Replies: 4
    Last Post: 01-25-2005, 07:31 PM

Tags for this Thread