简体   繁体   中英

How to check if a string from a text file is an int, or float, or none(string)?

given a textfile:

    123.33
    2
    1242.1
    99.9
    2223.11
    Hello
    22
    989.2
    Bye

How can i check if a line is integer or float or (none)array of chars? If it's an int then put it into the intArray, if it's double/float then put it into the doubleArray, else put it into the charArray.

FILE *file = fopen(argv[1], "r");
char line[100];

int intArray[100];
double doubleArray[100];
char charArray[100];

while(fgets(line, sizeof(line), file) != NULL){


}

The problem here is to distiguish between integers and floating-point numbers. A number with a decimal point or an exponent or both should be considered a float.

The old standard functions for numeric conversions are atoi for integers and atof for floats. These will parse the string, but may end up parsing it only halfway. atoi("123.4") is parsed as 123. On the other hand, atof(118) will (correctly) yield the floating-point number 118.0.

C99 provides more advanced parsing functions strtol (for long integers) and strtod (for doubles). These functions can return a tail pointer that points to the first character that wasn't converted, which allows you to find out whether the string was parsed completely or not.

With this we can write some straightforward wrapper functions that tell us whether a string represents an integr or a float. Make sure that you test for integers first, so that "23" is properly treated as integer:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int parse_double(const char *str, double *p)
{
    char *tail;

    *p = strtod(str, &tail);

    if (tail == str) return 0;
    while (isspace(*tail)) tail++;
    if (*tail != '\0') return 0;
    return 1;
}

int parse_long(const char *str, long *p)
{
    char *tail;

    *p = strtol(str, &tail, 0);

    if (tail == str) return 0;
    while (isspace(*tail)) tail++;
    if (*tail != '\0') return 0;
    return 1;
}

int main(void)
{
    char word[80];

    while (scanf("%79s", word) == 1) {
        double x;
        long l;

        if (parse_long(word, &l)) {
            printf("Integer %ld\n", l);
            continue;
        }

        if (parse_double(word, &x)) {
            printf("Double %g\n", x);
            continue;
        } 

        printf("String \"%s\"\n", word);
    }

    return 0;
}

you can do something like this

FILE *file = fopen(argv[1], "r");
char line[100];

int intArray[100];
double doubleArray[100];
char charArray[100];

int intNum;
float floatNum;
int i = 0; //index for intArray
int j = 0; //index for floatArray

while(fgets(line, sizeof(line), file) != NULL){
   intNum = atoi(line); 
  if (num == 0 && line[0] != '0') { //it's not an integer
     floatNum = atof(line);
     if(/* to put test condition for float */) {  //test if it is a float
         //add to floatArray    
     } else {
        //or strcpy to charArray accordingly
     }
  } else { //it's an integer
     intArray[i++] = intNum; //add to int array
  }       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM