Note that there are some explanatory texts on larger screens.

plurals
  1. POconverting a string to double using atof in c
    primarykey
    data
    text
    <p>I am trying to read from a file and store it in a matrix using c. The code I have is </p> <pre><code>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) &gt; 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 } </code></pre> <p>The code works well when I take p1 and convert it to integer using atoi. But when I use atof to convert the value to float, I am getting a zero. my input looks like</p> <pre><code>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 </code></pre> <p>After I read this input, when I print it I am getting</p> <pre><code> 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 </code></pre> <p>when I use atoi , I get the following </p> <pre><code>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 </code></pre> <p>Can someone please help me?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload