I have a problem, the data is transferred to the array, but not as much as I would like.

In general, I will describe my "path" and what I want to achieve. In the file P1Am.txt are data

1043.928480752930 1023.487233880130 1014.657596748670 1132.028858770790 1012.557090294320 1006.203887562610 1041.219702992490 1032.005532206760 1113.350002788500 1203.141773433670 980.619915778275 1061.732090096410 1129.337311152790 

and then so exactly 128 elements. I am quite normally laying out the elements in the variable obm-> stor-> AMP [k], but for some reason that number 1043.928480752930 turns into 1043, this rounding does not suit me.

 // test FILE *fid=fopen("P1Am.txt","rt"); if(fid) { float val=0; for(int k=0; k<128; k++) { fscanf(fid,"%f",&val); for(int n=0; n<obm->stor->NT; n++) { obm->stor->AMP[k*obm->stor->NT+n] = val; qDebug() << k+1 << obm->stor->AMP[k]; } } fclose(fid); } 

Even when I changed, on another one, the result was the same, or even worse.

  // test FILE *fid=fopen("P1Am.txt","rt"); if(fid) { double val=0; for(int k=0; k<128; k++) { fscanf(fid,"%.12d",&val); for(int n=0; n<obm->stor->NT; n++) { obm->stor->AMP[k*obm->stor->NT+n] = val; // амп я тогда тоже на double менял qDebug() << k+1 << obm->stor->AMP[k]; } } fclose(fid); } 

The result was the same.

Here I tried this option, the result is very upset (((sadness (((

  // test FILE *fid=fopen("P1Am.txt","rt"); if(fid) { double val=0; for(int k=0; k<128; k++) { fscanf(fid,"%d",&val); obm->stor->AMP1[k] = val; // создал новую перемен для теста AMP1 qDebug() << k+1 << obm->stor->AMP1[k]; } fclose(fid); } 

result:

  1 5.1531e-321 2 5.1531e-321 3 5.1531e-321 4 5.1531e-321 5 5.1531e-321 6 5.1531e-321 7 5.1531e-321 8 5.1531e-321 9 5.1531e-321 10 5.1531e-321 11 5.1531e-321 

What am I doing wrong ??

  • Is it possible that the fractional part separation will work correctly, and not with a full stop? - AivanF.
  • one
    And then qt ? Only because of qDebug ? Then, you can use QFile instead of FILE - gil9red

5 answers 5

Since you are using Qt, read the file with it:

 // test QFile fid("P1Am.txt"); if (fid.open(QIODevice::ReadOnly)) { double val = 0.0; while (!fid.atEnd()) { val = fid.readLine().toDouble(); // дальше ваш код (не проверял): for(int n=0; n<obm->stor->NT; n++) { obm->stor->AMP[k*obm->stor->NT+n] = val; qDebug() << k+1 << obm->stor->AMP[k]; } } } 

For maximum accuracy, use the double type.

Leave your code for storing values ​​in a structure unchanged.

PS: as already said, check the type obm-> stor-> AMP.

    First of all, for such accuracy, I would take a double type, not a float .

    Secondly, I have big doubts that the type obm->stor->AMP[k] is not integer ...

    Third, this is fscanf(fid,"%.12d",&val); - it's just a mockery of some kind ... It's hard to even imagine a combination of .12 and %d , and even with a double ... Something seems to me that it will be UB ...

    Or did I misunderstand your problem, and you want to get an integer value, just the rounding is not "mathematical"? Then the usual rounding is obtained by, for example,

     int k = std::round(f); 

    Well, or if C ++ 11 is not supported, you can play

     int k = floor(f+0.5); 

    since you have all positive values ​​...

      According to the first code, added output to the console:

       ... for(int k=0; k<14; k++) { fscanf(fid,"%f",&val); qDebug() << val; ... 

      Result:

       1043.93 1023.49 1014.66 1132.03 1012.56 1006.2 1041.22 1032.01 1113.35 1203.14 980.62 1061.73 1129.34 1129.34 

      The second option to get the value in general is always 0 returned.

      Why do you have these numbers without a fractional remainder of steel can only be guessed by representing what obm->stor->AMP

      • 1043.93 and I would like to 1043.928480752930. I slightly exaggerated. Purpose: maximum accuracy - timob256

      Show what type of AMP you have.

      As well as the conclusion:

       qDebug() << QLocale::system().name(); 

      for separators, apparently, the system locale is used, and it (I predict), you have Russian, for which the separator for the fractional link is,.

      If the output is locale Russian, and you need a dot in the separator, use QFile :

       QFile file("P1Am.txt"); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream ist(&file); ist.setLocale(QLocale::c()); // хотя, судя по документации, используется C локаль по умолчанию. while (!ist.atEnd()) { double val; ist >> val; for (size_t n = 0; n < obm->stor->NT; ++n) { obm->stor->AMP[k*obm->stor->NT+n] = val; // < непонятен какой тип у AMP, может там int? qDebug() << k+1 << obm->stor->AMP[k]; } } } 

        In addition, the answer is)) just "lf" - a long float helped me exactly 1043.928480752930.

          // test FILE *fid=fopen("P1Am.txt","rt"); if(fid) { double val=0; for(int k=0; k<128; k++) { fscanf(fid,"%lf",&val); obm->stor->AMP1[k] = val; qDebug("%.12f",obm->stor->AMP1[k]); // qDebug() << k+1 << obm->stor->AMP1[k]; } fclose(fid); }