![]() |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 2
| Recognize if the floating number is valid or not in C 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 | |
| | #2 |
| subminimalist 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;
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #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 | |
| | #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:
Code: && (*output = strtod(fnum, &end)), ( *end == '\n' || *end == '\0' ) -- 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 | |
![]() |
| Tags |
| c programming, file manipulation, floating point number |
| Thread Tools | |
| Display Modes | |
|
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 |