2016-03-22 28 views
0

Bir proje için farklı dizilerde bir metin dosyasının sütunlarını okumak istiyorum. Önce dosyayı bir 2B dize dizisinde okuyorum ve bu diziyi farklı int veya float 1D dizilerinde böldüm. Ancak, atof veya atoi yöntemiyle intfloat numarasına dönüştürdüğümde, bölümleme hatası aldım. Başka bir çözümü olan var mı?2d string dizilimi 1D'ye dönüştürür, int diziler

void Tftdiag::readFile(std::string file) 
{ 
    string testline; 
    char tab2[1024]; 
    strcpy(tab2, file.c_str()); 
    ifstream Test(tab2); 
if (!Test) 
{ 
    cout << "There was an error opening the file.\n"; 
} 
//store words in array 
int x=0,y=0; 
while(Test>>testline) 
{ 
    word[y][x]=testline; 
    x++; 
    if (testline=="") 
    y++; 
} 
//output whole array with array position numbers for each entry 
cout<<"Array contents:\n"; 
for (int y=0;y<50;y++) 
{ 
    for (int x=0;x<6;x++) 
    cout<<word[y][x]<<"("<<y<<","<<x<<")"<<endl; 
} 
for(int i=0; i<50; i++) { 

    voltage[i]= atof("0.5".c_str()); 
    //currentArray[i]= atof(word[50][1].c_str()); 
    //lux[i]= ::atof(word[50][2].c_str()); 
    //red[i]= atoi(word[50][3].c_str()); 
    //green[i]= atoi(word[50][3].c_str()); 
    //blue[i]= atoi(word[50][3].c_str()); 
} 
} 
+0

Şahsen, atoi() 'yerine std :: stoi()' yi kullanmayı tercih ederim. Deneyin, belki de – DimChtz

+0

'word''ün ilk boyutu 50'lik bir boyuta sahip olmanıza yardımcı olur. Böylece, yalnızca boyut - 1'e (49) kadar öğelere erişebilirsiniz. – Rakete1111

+1

'0,5 '' ifadesi bir nesne değildir, üye işlevini kullanamazsınız, bu nedenle derleyici hatası vermeniz gerekir. Aslında, "0,5" gibi bir dizgi, "char const *" türünde, "atof" türünün ilk elemanına bir işaretçi verecektir, böylece std'nin içine herhangi bir sarma kullanılmasına gerek yoktur. string nesnesi. –

cevap

0

Ben bir hata ayıklayıcı yayınlandıktan ve ben endeksi 0 zekâ tarafından değeri "7701" ve takip hattı ile bir hata var.

voltaj [i] = atof (sözcük [50] [0] .c_str());