简体   繁体   English

使用c中的atof将字符串转换为double

[英]converting a string to double using atof in c

I am trying to read from a file and store it in a matrix using c. 我试图从文件中读取并使用c将其存储在矩阵中。 The code I have is 我的代码是

ReadSparseInput (int nvtxs,int nsize, double  vector[], int rowptr[] ,int colind [] , double values[])
{
    int index,row=0,column=0;
    int sparsity_value, nvtxs_local;
    int VECTOR_SIZE =50;
    double value;
    double  randvect[VECTOR_SIZE];
    int  length=0;
    char buf[BUFSIZ];
    FILE *fp;
    fp=fopen("/home/lopez380/Parallel/m10-A.ij", "r");
    index =0;
    nvtxs_local = 0;
    rowptr[0] = 0;
    char *p1;
    while ( !feof(fp))
    {
         // null buffer, read a line
        buf[0] = 0;
        fgets(buf, BUFSIZ, fp);
        // if it's a blank line, ignore
        if(strlen(buf) > 1) 
        {
            p1 = strtok(buf, " ");
            row=atoi(p1);
            #ifdef DEBUG
                fprintf(stdout, "Row : %d ", row);
            #endif
            p1 = strtok(NULL, " ");
            column=atoi(p1);
            #ifdef DEBUG
                fprintf(stdout, "column : %d ", column);
            #endif
            p1 = strtok(NULL, " ");
            value=atof(p1);
            #ifdef DEBUG
                fprintf(stdout, "value : %g\n ", value);
            #endif
        }

    }
    fclose(fp);

    #ifdef DEBUG
    printf("lFileLen = %d\n", length);
    #endif

}

The code works well when I take p1 and convert it to integer using atoi. 当我使用p1并使用atoi将其转换为整数时,代码运行良好。 But when I use atof to convert the value to float, I am getting a zero. 但是当我使用atof将值转换为float时,我得到的是零。 my input looks like 我的输入看起来像

0 0 1.76420546156578
0 3 -4.80876443295632
0 6 1.20260376970926
0 9 3.30693451100554
1 1 1.13901264701619
1 4 0.370358438592131
1 7 -0.551593262343761

After I read this input, when I print it I am getting 在我阅读此输入后,当我打印它时,我得到了

 Row : 0 column : 0 value : 0
 Row : 0 column : 3 value : 0
 Row : 0 column : 6 value : 0
 Row : 0 column : 9 value : 0
 Row : 1 column : 1 value : 0
 Row : 1 column : 4 value : 0
 Row : 1 column : 7 value : 0

when I use atoi , I get the following 当我使用atoi时,我得到以下内容

Row : 0 column : 0 value : 1
 Row : 0 column : 3 value : -4
 Row : 0 column : 6 value : 1
 Row : 0 column : 9 value : 3
 Row : 1 column : 1 value : 1
 Row : 1 column : 4 value : 0
 Row : 1 column : 7 value : 0

Can someone please help me? 有人可以帮帮我吗?

I would strongly discourage you from using atoi() and atof(), unless your implementation provides error checking (eg atoi() and atof()" update errno . 我强烈反对你不使用atoi()和atof(),除非你的实现提供错误检查(例如atoi()和atof()“update errno

I prefer fscanf(): illegal input can be detected by the return value. 我更喜欢fscanf():非法输入可以通过返回值检测到。

EXAMPLE: 例:

if (fscanf(fp, "%f", &f) == 1)
    ...all OK...
else
    ...EOF or conversion failure...

For reasons mentioned above, replace 由于上述原因,请更换

value = atof(p1);

...with... ...与...

if( sscanf(p1,"%s",&value) != 1) { 
    fprintf(stderr,"Error: non-number found!\n");
    exit(1); 
}

The solution as paulsm4 described in the comment is 评论中描述的paulsm4解决方案是

 fp=fopen("/home/lopez380/Parallel/m10.vec", "r");
    int i=0;
    double val;
    while ( !feof(fp))
    {
        buf[0] = 0;
        fgets(buf, BUFSIZ, fp);
        if(strlen(buf) > 1) 
        {
            p1 = sscanf(buf, "%lf",&val );
            vector[0]=val;
            ++i;
            #ifdef DEBUG
                fprintf(stdout, "vector : %lf \n", vector[0]);
            #endif
        }
    }
    fclose(fp);

Output comes to 输出来了

vector : 0.837565
vector : 3.074878
vector : -3.968584
vector : 3.975388
vector : 0.703345

TO read values (separated by space )into multiple arrays I used 将值(由空格分隔)读入我使用的多个数组中

 sscanf(buf, "%d%d%lf",&row,&column,&value );

and buf has a datatype 和buf有一个数据类型

char buf[BUFSIZ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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