如何检查是否从一个文本文件中的字符串是int,或float或无(字符串)? [英] How to check if a string from a text file is an int, or float, or none(string)?

查看:188
本文介绍了如何检查是否从一个文本文件中的字符串是int,或float或无(字符串)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个文本文件:

  123.33
    2
    1242.1
    99.9
    2223.11
    你好
    22
    989.2
    再见

我怎么能检查是否行是整数或浮点数或字符的(无)阵列?如果它是一个整数,那么把它放到intArray,如果是双/浮,然后把它放到的doubleArray,否则把它放到charArray。

  FILE *文件=的fopen(的argv [1],R);
焦线[100];INT intArray [100];
双的doubleArray [100];
炭charArray [100];而(与fgets(线,的sizeof(线),文件)!= NULL){
}


解决方案

这里的问题是整数和浮点数之间distiguish。许多带有小数点或指数或两者应被视为一个浮动。

有关数字转换旧标准功能的atoi 整数和 ATOF 的花车。这些将解析字符串,但最终可能会分析它只能半途而废。 的atoi(123.4)被解析为123.在另一方面, ATOF(118)将(正确)产生浮点数118.0。

C99提供了更高级的分析函数与strtol (长整型)和的strtod (双打)。这些功能可以返回一个指向第一个字符未转换,它可以让你找到了该字符串是否被彻底与否解析的尾指针。

有了这个,我们可以写一些简单的包装函数,告诉我们一个字符串,再$ P $是否psents的INTEGR或浮动。请确保您测试整数第一,让23正确的整数处理:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&文件ctype.h GT;INT parse_double(为const char * str中,双击* P)
{
    字符*尾;    * P =的strtod(STR,&安培;尾);    如果(尾==海峡)返回0;
    而(isspace为(*尾))尾++;
    如果(*尾='\\ 0'!)返回0;
    返回1;
}INT parse_long(为const char * str中,长* P)
{
    字符*尾;    * P =与strtol(STR,&安培;尾,0);    如果(尾==海峡)返回0;
    而(isspace为(*尾))尾++;
    如果(*尾='\\ 0'!)返回0;
    返回1;
}INT主要(无效)
{
    字符字[80];    而(scanf的(%辆79,字)== 1){
        双X;
        长升;        如果(parse_long(文字,&安培; L)){
            的printf(整数%LD \\ n,L);
            继续;
        }        如果(parse_double(文字,&安培; X)){
            的printf(双重%G \\ n,x)的;
            继续;
        }        的printf(字符串\\%s \\的\\ N字);
    }    返回0;
}

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;
}

这篇关于如何检查是否从一个文本文件中的字符串是int,或float或无(字符串)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆