C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-15-2009, 10:59 AM   #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);
}
feb0590 is offline   Reply With Quote
Old 04-15-2009, 02:47 PM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 04-16-2009, 04:49 AM   #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);

}
feb0590 is offline   Reply With Quote
Old 04-16-2009, 04:57 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Reply

Tags
c programming, file manipulation, floating point number

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with this compiler error Evangeline C Programming 7 04-05-2008 09:27 AM
HELP! Changing the base of a floating point number.. yigster C Programming 6 03-27-2008 05:36 AM
Logical errors with seach function Taka C Programming 4 09-18-2006 05:20 AM
Combining four hex characters into one floating point number for output? lava C Programming 14 04-06-2006 12:18 AM
problem printing with floating number ssharish C++ Programming 4 01-25-2005 07:31 PM


All times are GMT -6. The time now is 09:30 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22