【问题标题】:read Exponential number from a text file and convert it to INT64 in vc++从文本文件中读取指数并在 vc++ 中将其转换为 INT64
【发布时间】:2013-02-20 07:48:44
【问题描述】:

您好,我的问题看起来很简单,但我正在尝试从文本文件中读取大量数字,但未能正确执行!!!

看看file1.text中保存的以下文件

我的结构如下:

struct PDW_FileFormat
{
    float TOA;
    float Freq;
    float  PW;  
    float  PA[10];  
    float  PASlc1;
    float  PASlc2;
    float  PAOmni;
    float  AZ_Angle;
    float  EL_Angle;
    float EST_Az_Angle;
    float EST_El_Angle;
    float Cdiff;
    unsigned int index_in_packet;

};

并像下面的代码一样打开和读取文件

PDW_FileFormat filedata;
    unsigned index_in_packet=0;
p = fopen("file1.txt" ,"r");
while (!feof(p))
    {
        memset(&filedata,0,sizeof(PDW_FileFormat));
        fscanf(p ,"%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f"
                      ,&filedata.TOA    //1
                      ,&filedata.Freq   //2
                      ,&filedata.PW     //3
                      ,&filedata.PA[0]  //4
                      ,&filedata.PA[1]  //5
                      ,&filedata.PA[2]  //6
                      ,&filedata.PA[3]  //7
                      ,&filedata.PA[4]  //8
                      ,&filedata.PA[5]  //9
                      ,&filedata.PA[6]  //10
                      ,&filedata.PA[7]  //11
                      ,&filedata.PA[8]  //12
                      ,&filedata.PA[9]  //13
                      ,&filedata.PASlc1 //14
                      ,&filedata.PASlc2 //15
                      ,&filedata.PAOmni //16
                      ,&filedata.AZ_Angle   //17
                      ,&filedata.EL_Angle); //18

          filedata.index_in_packet = index_in_packet;
          // some code to convert a value like
              // 1.386500000000000e+10
              // to 
              // 138650000000000

              INT64 TOAConvert;
              //**your code or your answer **
          index_in_packet++;
     }

我已经尝试过这些代码,但对我来说效果不佳

//filedata.TOA = 1.3865000e+010
              INT64 variabletoConvert;
          stringstream ss;
          ss.setf(ios::fixed);
          ss.precision(0);
          ss << filedata.TOA;
          ss >> variabletoConvert;
          // variabletoConvert = 13864999936 i expect 138650000000000
          INT64 variabletoConvert2;
          variabletoConvert2 = static_cast<INT64>(filedata.TOA);
          //variabletoConvert2 = 13864999936 i expect 138650000000000   
          INT64 variabletoConvert3;
          variabletoConvert3  = (INT64)(filedata.TOA);
          //variabletoConvert3 = 13864999936 i expect 138650000000000 

看看这个调试图像:

【问题讨论】:

    标签: c++ file visual-c++ memory type-conversion


    【解决方案1】:

    好吧,我已经完成了这个解决方案,但它涵盖了巨大数字的一部分,如果有人发布了一个检索完整数字的解决方案,我会将其标记为答案是否有可能恢复一个强制保存在 4 字节中的数字漂浮……!!

    #include "stdafx.h"
    #include <iostream>
    #include <math.h>
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {
        float inputvalue;
        std::cout <<"Enter a large number" <<endl
                  <<"system will save it in a float variable" <<endl
                  <<"Your Number =";
        std::cin >> inputvalue;
        float temp;
        temp = inputvalue;
        int numberDigits =0;
        double DivisionCount= 1;
        while (temp > 1)
        {
            numberDigits++;
            DivisionCount*=10;
            temp = temp / 10;
        }
        temp = inputvalue;
        int middlevar=0;
        double Outputvalue=0;
        for (int i =0;i < numberDigits ; i++)
        {
        DivisionCount /= 10;
        middlevar =temp / DivisionCount;
        temp = temp - (middlevar * DivisionCount);
        Outputvalue *= 10;
        Outputvalue+= middlevar;
        }
         cout << endl << "Your value saved in memmory"<<Outputvalue;
        cin.get();
        cin.get();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-02
      • 2021-05-20
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 2016-12-20
      相关资源
      最近更新 更多