2012-06-15 5 views
0

읽기 .CSV 과학적 표기법은 데이터의 예는 다음과 같습니다내가 여기이 .csv 파일을 읽으려고하고 있어요

1.33286E+12 0 -20.790001 -4.49 -0.762739 -3.364226 -8.962189 

1.33286E+12 0 -21.059999 -4.46 -0.721878 -3.255263 -8.989429 

문제는 Excel에서 첫 번째 열에 행 1과 2를 함께 제출할 셀의 숫자는 1.33286E + 12로 표시되며 셀을 클릭하면 1332856031313과 1332856031328이지만 프로그램은 1.33286E + 12로 읽음을 나타내지 만 전체 숫자는 1332856031313과 1332856031328이 필요합니다.

코드 :

inputfile.open(word1.c_str()); 
while (getline (inputfile, line)) //while line reads good 
{ 
    istringstream linestream(line); //allows manipulation of string 
    string str; 

while (getline (linestream, item, ',')) //extract character and store in item until ',' 
    { 

    char * cstr, *p; 
    cstr = new char [item.size()+1]; 
    strcpy(cstr, item.c_str()); 
    p = strtok(cstr, " "); 

    while (p!=NULL) //while not at the end loop 
    {  // double e = atof(p); // coverts p to double 
     value++; 
     if(value == 1) 
       {  double e = atof(p); // coverts p to double 
      if(m ==1) 
      cout << time[0]<<"\n"; 

      ostringstream str1; 
      str1 << e; 
      str = str1.str(); 
      string str2; 
      str2.append(str.begin(), str.end()); 
      const char * convert = str2.c_str(); 
      e = atof(convert); 
     time[m] = e*0.001; 
      m++; 
      //if(m >=192542) 
      //cout << time[m-1]<<"\n"; 

     } 

       p = strtok(NULL, " "); 
    } 
    delete[] cstr; //delete cstr to free up space. 
} 
count ++; 
value = 0; 
} 
inputfile.close(); 
+2

문제는 독자가 아니며 작가에게 있습니다. 완전 정확도로 값을 출력하려면 CSV 생성기 (Excel, 아마도?)를 가져와야합니다. – Rook

답변

2

숫자 1332856031313이 1.33286E + 12로 직렬화되면 직렬화 과정에서 다시 얻을 수있는 방법이 없습니다. 그 6 개의 유효 자릿수 형태의 정보는 영원히 사라졌습니다. CSV 파일이 생성 될 때 전체 정밀도로 저장되는지 확인해야합니다. Excel로이 작업을 수행하는 방법을 모르겠습니다.

또한 atofconst char*은 매우 C++ - esque가 아닙니다.

double a, b, c, d; 
linestream >> a >> b >> c >> d; 

과 같은 코드를 사용하는 것이 좋습니다.

+0

팁 주셔서 감사합니다! 이제는 의미가 있습니다. – CIM

0

루크는 나를 맞았지만 한 가지 제안을하겠습니다. 문자열 스트림 상태에 대한 테스트, 디코딩을 위해 루프를 사용하십시오. 좋습니다, 두 가지 제안 : 코드를 함수로 분리하십시오. 그것을 하나의 큰 덩어리에 모두 넣으면 이해하기가 더 어려워집니다.

void DecodeLine(const std::string &sLine, 
        std::vector<double> &vResults) 
    { 
     std::istringstream istr(sLine); 
     double d = 0; 
     istr >> d; 
     while (!istr.fail()) 
     { 
      vResults.push_back(d); 
      istr >> d; 
     } 
    } 
관련 문제